When to minify JSON
Minifying makes sense whenever bytes cost: network traffic, database storage, JSON columns in MySQL or Postgres, WebSocket payloads, queue messages on SQS or Kafka. Every whitespace byte you drop is one less byte your system processes, stores or transmits. At scale (millions of requests a day) it shows up on your cloud bill.
When NOT to minify
- Config files versioned in git. A minified package.json produces unreadable diffs and merge conflicts become a nightmare.
- Logs. Even with pretty-printers around, reading minified logs by eye in a terminal is rough.
- Data a human will edit. If someone's expected to hand-edit the file, leave it formatted.
- When gzip/brotli is already on. The server compresses on the fly, narrowing the gap between minified and formatted. Still worth minifying first to ease parsers.
How minification works
The process is trivial: parse the JSON with JSON.parse and re-serialize with JSON.stringify(obj) without the third indentation argument. The result is canonical JSON with no whitespace outside strings. Strings are preserved as-is: if a string contains a space, it stays. Only structural whitespace is stripped.
Further compression
Minifying is step one. To go smaller:
- Gzip or Brotli over HTTP. Your web server (nginx, Cloudflare, Vercel) does this by default. Reduces JSON another 60-80%.
- Binary formats. If you control client and server, MessagePack, CBOR or Protocol Buffers are substantially smaller.
- Renaming keys. If an API returns millions of objects keyed
"longDescriptiveKey", shortening to"k"saves a lot. Costs readability and requires a mapping back on the client.
Typical before/after sizes
A typical e-commerce object with 20 fields and 2-space indentation runs about 800 bytes. Minified drops to 600. Minified and gzipped lands at 250-300. If you're returning lists of a thousand objects per request, that 50% adds up in latency and bandwidth cost.
Validation included
This minifier parses with JSON.parse, so syntax errors are flagged. If your input has comments or trailing commas (which strict JSON rejects), it fails. For non-standard input run a linter first.
Privacy and sensitive data
Because the minifier runs in your browser, you can paste configs with tokens, real user data, or anything sensitive without risk. Nothing leaves your device. If unsure, open DevTools and watch the Network panel: you'll see no outbound requests while minifying.