Whitespace Removal vs Gzip
These are different layers and they compound. Whitespace removal is lossless at the JSON level and typically saves 20–60% of raw bytes. Gzip or Brotli then compresses what remains at the transport level. Minifying first gives the compressor less redundancy to encode, so the compressed result is usually smaller too — though by a narrower margin than the raw saving suggests.
What Compression Does Not Change
- Whitespace inside string values —
{"note": "two spaces"}is preserved exactly. - Key order, array order, numeric precision, and null values.
- Unicode escapes and the document's semantic meaning.
Compressing JSON in Code
# jq, compact output
jq -c . input.json > output.min.json
# Python
python3 -c "import json,sys; json.dump(json.load(sys.stdin), sys.stdout, separators=(',',':'))"
// Node.js
JSON.stringify(JSON.parse(raw));
Where the Savings Come From
Deeply nested, heavily indented documents save the most, because every level of nesting adds indentation to every line. A flat array of short objects saves the least. Documents dominated by long string values barely shrink at all — there is little whitespace between tokens to remove.
Who this guide is for
- Cut the size of a JSON response before it goes over a mobile connection
- Shrink a config bundle shipped inside a JavaScript build
- Reduce the footprint of documents written to a cache or a JSON column
- Trim a fixture file that bloated a repository