@adityashah.work/jsonclean
v0.4.1
Published
Clean and normalize JSON for readable files and quieter git diffs.
Maintainers
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
nullvalues - remove empty strings, arrays, and objects
- sort object keys recursively
- format JSON consistently for commits, fixtures, logs, and configs
Install
npm install @adityashah.work/jsoncleanOr try it directly:
npx @adityashah.work/jsonclean input.jsonCLI
Basic usage:
json-clean input.jsonWrite to a file:
json-clean input.json -o output.jsonWrite back to the same file:
json-clean input.json --writeCheck whether a file is already normalized:
json-clean input.json --checkPipe from stdin:
cat input.json | json-cleanClean multiple files with a glob:
json-clean "fixtures/**/*.json" --writeUse project defaults from a config file:
json-clean "fixtures/**/*.json" --checkCommon 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.jsonjsonclean.config.jsonpackage.jsonunder thejsoncleankey
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.jsonAPI
cleanJson(input, options?)
Returns a cleaned JSON-compatible value.
Options:
sortKeysdefault:trueremoveNulldefault:trueremoveEmptydefault:trueremoveUndefineddefault: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:
indentdefault:2finalNewlinedefault:true
