Minify a JSON file
Strip whitespace and indentation from a JSON file to produce the smallest valid output.
How to minify a json file in each shell
jq -c . data.json`jq -c` (compact) emits one JSON value per line with no whitespace. The `.` filter is identity — outputs the input unchanged structurally. For multi-document JSON (one object per line): `jq -c .[] data.json` flattens an array into newline-delimited JSON (NDJSON). Output preserves key ORDER from input unless you pass `-S` (sort keys alphabetically) — `-S` is useful for deterministic diffs of minified output.
jq -c . data.jsonSame external `jq`. macOS `brew install jq` ships v1.7+. For pure Python (no jq dep): `python3 -c "import json,sys; json.dump(json.load(open('data.json')), sys.stdout, separators=(',',':'))" > min.json` — the explicit `separators` strips the default space-after-comma + space-after-colon that `json.dumps` emits.
jq -c . data.jsonSame external. Fish capture: `set -l minified (jq -c . data.json)`. Fish has no JSON builtin — relies on `jq` / `python`.
Get-Content data.json -Raw | ConvertFrom-Json | ConvertTo-Json -Compress -Depth 100`-Compress` strips whitespace. **`-Depth 100` is mandatory** — pwsh `ConvertTo-Json` truncates nested objects past 2 (5.1) / 100 (7+) levels by default, REPLACING the deep structure with type-name strings like `"System.Collections.Hashtable"`. The truncation is SILENT — the output is valid JSON but wrong. Always pass `-Depth 100` (or higher) when minifying input you didn't create yourself. Also note `ConvertTo-Json` rewrites dates as ISO-strings + numbers as `[long]`/`[double]` — may change wire format.
powershell -Command "Get-Content data.json -Raw | ConvertFrom-Json | ConvertTo-Json -Compress -Depth 100"cmd has no JSON builtin. Shell out to pwsh (built-in Windows 10+). For one-off non-Windows-10 use: install Python 3 → `python -c "import json,sys; json.dump(json.load(sys.stdin), sys.stdout, separators=(',',':'))" < data.json > min.json`.
Equivalents listed for Bash, Zsh, Fish, PowerShell, cmd.exe.
Gotchas & notes
- **pwsh `-Depth` default is the trap**: pwsh 5.1 `ConvertTo-Json` default `-Depth 2`. pwsh 6/7 default `-Depth 100`. If you minify a 3-deep object on pwsh 5.1 WITHOUT `-Depth`, the level-3 contents become `"@{key=value}"` (hashtable's ToString) embedded as a STRING — valid JSON, silently wrong. The pwsh 5.1 default is the single biggest source of "my JSON minifier broke my config" reports. Pass `-Depth 100` defensively, every time.
- **Floating-point + number representation**: minifying a JSON with `"price":3.14` may round-trip through pwsh `[double]` and emit `3.14` unchanged — or emit `3.1400000000000001` (binary-FP artifact). For exact preservation of numeric strings: use `jq -c` (preserves source string representation) instead of pwsh (re-formats via .NET `[double]`). Python `json.dumps(json.loads(...))` similarly may emit `3.14` as `3.14` for short forms but adds digits for longer ones.
- **NDJSON / streaming minify**: log shippers and event pipelines emit ONE JSON value per line. `jq -c "."` on a SINGLE-DOC input collapses to one line; `jq -c ".[]"` on an array input emits ONE line per array element (= NDJSON). For multi-document INPUT (concatenated JSON values, no array): `jq -cs . input.json` (`-s` = slurp, collects into array first), or for true streaming: `jq -c "." < multi.jsonl` works because each line is already independent.
- **Validation as a side-effect of minification**: `jq -c . input.json` FAILS on invalid input with `parse error: ...`. Use exit code: `jq -c . input.json > min.json && echo OK || echo BROKEN`. pwsh `ConvertFrom-Json` similarly throws on parse error. Both serve as accidental JSON validators — preferable to a `grep` regex for `:` and `{` matching.
Related commands
Related tasks
- Parse JSON from a shell— Extract specific fields from a JSON blob (a curl response, kubectl output, log line) and pipe them into other shell tools — the everyday "grab the .token / .items[].name / .data.url" workflow that differs sharply from just pretty-printing.
- Format (pretty-print) JSON from a shell— Pretty-print a JSON blob from a curl response, log line, or config file — for human-readable inspection or diffing.
- Pretty-print an XML file— Indent and format an XML document for human reading.