jsonfix-cli
v1.0.1
Published
Fix broken JSON: remove trailing commas, convert single quotes, strip comments, quote unquoted keys, and more.
Maintainers
Readme
jsonfix
A CLI tool that fixes common JSON issues. No more hand-editing broken JSON from config files, API responses, logs, or copy-pasted snippets.
What it fixes
- Trailing commas —
{"a": 1, "b": 2,}becomes valid - Single quotes —
{'key': 'value'}converted to double quotes - Comments —
// lineand/* block */comments stripped - Unquoted keys —
{name: "val"}gets proper key quoting - Missing closing brackets — best-effort bracket completion
Install
npm install -g jsonfix-cliOr use without installing:
npx jsonfix-cli broken.jsonUsage
Fix a file
jsonfix broken.jsonPipe from stdin
echo "{ name: 'test', }" | jsonfixcat config.jsonc | jsonfixWrite output to a file
jsonfix broken.json -o fixed.jsonCheck if JSON is valid (no output, exit code only)
jsonfix --check config.json
# Exit code 0 = valid, 1 = invalidMinify output
jsonfix data.json --minifyCustom indentation
jsonfix data.json --indent 4Combine flags
cat api-response.txt | jsonfix --indent 4 -o clean.jsonFlags
| Flag | Short | Description | Default |
|------|-------|-------------|---------|
| --output <file> | -o | Write output to a file instead of stdout | — |
| --check | -c | Validate only; exit 0 if valid, 1 if not | false |
| --minify | -m | Minify the output (no whitespace) | false |
| --indent <n> | -i | Number of spaces for indentation | 2 |
| --help | -h | Show help message | — |
| --version | -v | Show version number | — |
Exit codes
| Code | Meaning |
|------|---------|
| 0 | Success (or valid JSON with --check) |
| 1 | Invalid JSON that could not be fixed (or invalid with --check) |
| 2 | File not found or I/O error |
Examples
Input:
{
// Database config
host: 'localhost',
port: 5432,
options: {
ssl: true,
timeout: 30, /* seconds */
},
}Output:
{
"host": "localhost",
"port": 5432,
"options": {
"ssl": true,
"timeout": 30
}
}Zero dependencies
jsonfix uses only Node.js built-in modules. Nothing extra to install, audit, or worry about.
License
MIT
