Skip to content
Back to Blog
By JSONConvert Team··6 min read

How to Minify JSON (and When Not To)

Pretty-printed JSON is great for reading and terrible for shipping. Every space, newline, and indent is a byte that travels over the wire, sits in a database column, or eats into a localStorage quota. Minifying strips all of that out and leaves the smallest valid JSON that still parses to the same data. The how is one line of code. The when is where most developers get it wrong.

What Minifying JSON Actually Removes

Minification removes insignificant whitespace: the indentation, the spaces after colons and commas, and the line breaks between keys. It does not touch the data itself. Keys keep their names, strings keep their contents, and the parsed result is byte-for-byte identical.

Take this object:

That is 171 bytes. Minified, it becomes 122 bytes:

Same data, 29% smaller. The savings come entirely from whitespace that JSON parsers ignore anyway. JSON.parse does not care whether there is a space after the colon, so dropping it costs you nothing at read time.

Minifying in JavaScript and Node.js

The browser and Node both give you minification for free through JSON.stringify. Call it with a single argument and you get compact output by default:

The trick most people miss is that pretty-printing is the opt-in, not the default. The third argument controls indentation:

If you already have a JSON string and want to minify it, parse and re-stringify. This also validates the input as a side effect, so malformed JSON throws instead of silently passing through:

On the command line, jq -c does the same thing:

For a quick one-off without writing code, paste it into the JSON minifier and copy the result. When you need to go the other way - reading a minified blob someone handed you - the JSON beautifier re-indents it so you can actually see the structure.

The Gzip Catch Nobody Mentions

Here is the part that changes how you should think about this. If your server sends responses with gzip or brotli compression - and almost every server does - minifying JSON barely moves the needle on what actually travels over the network.

Compression algorithms are very good at collapsing repeated whitespace. The newlines and indentation that minification removes are exactly the kind of repetitive pattern gzip eliminates for nearly free. I ran a 200-object array through both:

StagePretty-printedMinifiedReduction
Raw bytes16,80810,80736%
After gzip1,6961,6503%
The raw saving looks huge at 36%. After gzip, minifying buys you 3%. The compressor was already doing the heavy lifting.

So the size argument for minifying API responses is weaker than it looks, as long as compression is on. Check your response headers for content-encoding: gzip before you spend effort minifying a payload. If it is there, the network win is marginal.

When Minifying Is Worth It

Minification still earns its place in a few spots where compression does not help or is not present:

  • Storing JSON in a database column or a cell - no gzip layer, so the raw bytes are what you pay for
  • localStorage and sessionStorage, which have a hard quota measured in raw characters
  • Embedding JSON in a URL, a cookie, or an HTML data attribute, where every character counts and compression is not in play
  • Inline JSON in a generated HTML page or a config bundled into a build artifact
  • Reducing the size before you Base64-encode it, since Base64 adds a 33% overhead on top of whatever you feed it
In all of these the bytes are stored or transmitted uncompressed, so the 30-something percent you save is real and permanent.

When You Should Not Minify

Do not minify JSON you need to read or diff. A minified config file is unreviewable in a pull request - every change shows up as one giant modified line, and code review tools cannot show you what moved. Keep config files, fixtures, and anything checked into git pretty-printed. Storage is cheap; a reviewer squinting at a 4,000-character single line is not.

Logs are the same. If you write structured JSON logs and ever expect a human to read them with grep or tail, the readability of one-object-per-line pretty output usually beats the bytes you would save.

The clean split: minify what machines transmit and store, pretty-print what humans read. Build steps and runtime serialization should produce compact JSON. Source files in your repo should stay formatted.

A Practical Workflow

The safe pattern is to keep your source JSON readable and minify as a build or serialization step, never by hand-editing files into one line. If you are doing it manually for a one-off - shrinking a payload to fit in a URL or a storage cell - validate first so you do not minify broken JSON. Run it through the JSON validator to catch a trailing comma or unquoted key, then minify with the JSON minifier.

One last gotcha: minifying does not deduplicate or reorder keys, and it will not warn you about a 50 MB payload that is large because of the data, not the formatting. If your JSON is big after minifying, the problem is the structure, not the whitespace - and that is a schema conversation, not a formatting one.

Related Tools