JSPM

@adityashah.work/jsonclean

0.4.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 20
  • Score
    100M100P100Q53758F
  • License MIT

Clean and normalize JSON for readable files and quieter git diffs.

Package Exports

  • @adityashah.work/jsonclean

Readme

json-clean

Clean and normalize JSON for readable files and quieter git diffs.

Why

json-clean is a tiny CLI and library for the cleanup that formatters do not handle:

  • remove null values
  • remove empty strings, arrays, and objects
  • sort object keys recursively
  • format JSON consistently for commits, fixtures, logs, and configs

Install

npm install @adityashah.work/jsonclean

Or try it directly:

npx @adityashah.work/jsonclean input.json

CLI

Basic usage:

json-clean input.json

Write to a file:

json-clean input.json -o output.json

Write back to the same file:

json-clean input.json --write

Check whether a file is already normalized:

json-clean input.json --check

Pipe from stdin:

cat input.json | json-clean

Clean multiple files with a glob:

json-clean "fixtures/**/*.json" --write

Use project defaults from a config file:

json-clean "fixtures/**/*.json" --check

Common options:

json-clean input.json --indent 4
json-clean input.json --keep-null --keep-empty
json-clean input.json --no-sort
json-clean input.json --minify
json-clean input.json --diff
json-clean input.json --write
json-clean input.json --check
json-clean "fixtures/**/*.json" --write
json-clean "fixtures/**/*.json" --check

--diff prints a unified before/after patch to stdout. If you also pass --output, the cleaned JSON is still written to the output file.

--write updates the input file in place. --check is useful in CI and exits with code 1 when a file is not normalized.

Glob patterns are supported for multi-file workflows. When you pass multiple files or patterns, use --write, --check, or --diff.

Config

json-clean can load project defaults from the current working directory or any parent directory.

Supported locations:

  • .jsoncleanrc
  • .jsoncleanrc.json
  • jsonclean.config.json
  • package.json under the jsonclean key

Example .jsoncleanrc:

{
  "sortKeys": true,
  "removeNull": true,
  "removeEmpty": true,
  "indent": 2,
  "minify": false
}

CLI flags always override config values.

Example package.json:

{
  "name": "my-project",
  "jsonclean": {
    "sortKeys": false,
    "removeNull": true,
    "indent": 4
  }
}

Library

import { cleanJson, cleanJsonString } from "@adityashah.work/jsonclean";

const data = {
  b: null,
  a: 1,
  c: {
    z: "",
    y: 2,
  },
};

console.log(cleanJson(data));
console.log(cleanJsonString(JSON.stringify(data)));

If you install the package globally, the CLI command stays simple:

json-clean input.json

API

cleanJson(input, options?)

Returns a cleaned JSON-compatible value.

Options:

  • sortKeys default: true
  • removeNull default: true
  • removeEmpty default: true
  • removeUndefined default: true

cleanJsonString(input, cleanOptions?, formatOptions?)

Parses a JSON string, cleans it, and returns formatted JSON text.

formatJson(input, formatOptions?)

Formats a JSON-compatible value.

Format options:

  • indent default: 2
  • finalNewline default: true