Why Minify JSON?
Every space, newline, and tab in a formatted JSON file is an extra byte that must be transmitted over the network, stored in memory, and written to disk. In development, that whitespace is priceless — it makes the data readable and debuggable. In production, it is pure overhead.
Consider a typical API response with 50 fields and 3 levels of nesting, formatted with 2-space indentation. The whitespace alone might add 15–30% to the payload size. For an API that handles 10 million requests per day, minifying the responses can save terabytes of bandwidth per month and measurably reduce server response times.
JSON Minification in Code
JavaScript / Node.js
// Minify a formatted JSON string
const formatted = '{ "name": "Alice", "scores": [98, 87, 92] }';
const minified = JSON.stringify(JSON.parse(formatted));
console.log(minified);
// Output: {"name":"Alice","scores":[98,87,92]}
// Minify an object directly
const obj = { name: 'Bob', active: true, roles: ['admin'] };
console.log(JSON.stringify(obj));
// Minify a JSON file in Node.js
const fs = require('fs');
const data = JSON.parse(fs.readFileSync('pretty.json', 'utf-8'));
fs.writeFileSync('minified.json', JSON.stringify(data));Python
import json
formatted = '{"name": "Alice", "scores": [98, 87, 92]}'
# separators removes optional whitespace
minified = json.dumps(json.loads(formatted), separators=(',', ':'))
print(minified)
# Output: {'name':'Alice','scores':[98,87,92]}
# Minify and write to file
with open('pretty.json') as f:
data = json.load(f)
with open('minified.json', 'w') as f:
json.dump(data, f, separators=(',', ':'))
# One-liner from command line:
# python3 -m json.tool --no-indent data.jsonCommand Line
# jq -c flag produces compact (minified) output
jq -c . data.json
# Pipe curl response through jq to minify
curl -s https://api.example.com/data | jq -c .
# Python one-liner (no jq needed)
python3 -c "import json,sys; d=json.load(sys.stdin); print(json.dumps(d,separators=(',',':')))" < data.json
# Node.js one-liner
node -e "const fs=require('fs'); process.stdout.write(JSON.stringify(JSON.parse(fs.readFileSync('data.json','utf8'))))"Size Reduction Benchmarks
| JSON type | Formatted (2-space) | Minified | Savings |
|---|---|---|---|
| Simple flat object (5 fields) | 168 bytes | 108 bytes | 36% |
| API response (3 levels, 20 fields) | 1.2 KB | 820 bytes | 32% |
| OpenAPI spec (medium) | 24 KB | 16 KB | 33% |
| Deep-nested config (6+ levels) | 8 KB | 4.4 KB | 45% |
Actual savings vary by indentation size (4-space saves more than 2-space) and nesting depth. After gzip compression, the difference narrows (both compress similarly well).
Minification vs Compression: What's the Difference?
Minification and compression are complementary but different:
- Minification — removes whitespace from the human-readable source. Permanent: the minified form is still valid JSON. Reduces file size by 20–50%. Done at development/build time. The application reads minified JSON directly.
- Compression (gzip/Brotli) — encodes bytes to a smaller binary representation. Transparent: the application decompresses before reading. Reduces file size by 70–90% (JSON text has very high repetition, so compresses extremely well). Done by the web server/CDN automatically.
Best practice: minify your JSON first, then rely on server-side compression. Minified JSON + gzip is typically 85–95% smaller than the original formatted JSON.
When NOT to Minify
Avoid minifying JSON in these situations:
- Config files in version control— diffs on minified JSON are unreadable ("1 line changed" when 20 fields were touched). Keep readable, sort keys consistently, and use a linter like eslint-plugin-json.
- Fixture data for tests — test fixtures need to be readable by developers updating them. Formatted JSON makes the intent of the fixture clear.
- Any JSON a human will edit — if a developer or content editor will open and modify the file, formatted is always better.
Key Facts
- JavaScript:
- JSON.stringify(obj) — no space arg
- Python:
- json.dumps(data, separators=(',', ':'))
- jq:
- jq -c . data.json
- Typical size saving:
- 20–50% (before gzip)
- With gzip on top:
- 85–95% smaller than formatted+gzip
- Privacy:
- All in-browser — JSON never uploaded
Related JSON Tools
- Format JSON Online — the opposite: expand minified JSON
- JSON Beautifier Online — beautify ugly JSON
- Pretty Print JSON — multi-language pretty-print examples
- Dedicated JSON Minifier — standalone minification tool
- JSON Validator — validate without formatting or minifying
- JSON Diff Checker — compare two JSON objects