About JSON Minify
Minifying JSON removes the whitespace between tokens - indentation, newlines, the space after a colon. Nothing about the data changes, because that whitespace was never significant; only the byte count does.
The saving is real but modest, and it is worth knowing where it actually comes from. Pretty-printed JSON is typically 15-30% whitespace, so minifying cuts that. Gzip or Brotli, which every HTTP server should already be applying, compresses repeated indentation extremely well - so minifying a response that is already gzipped saves far less than the raw numbers suggest. Minify because it is free, not because it is transformative.
Where minification does matter is in places without transport compression: a JSON value stored in a database column, an environment variable, a URL query parameter, a cookie, a log line, or a message on a queue with a size limit. Those pay for every byte.
The one thing minification will not do is shorten your keys, and in a large array of objects the repeated key names are usually a bigger share of the payload than the whitespace ever was. If size genuinely matters at that point, the structural fix is a columnar shape - keys once, values in arrays - rather than more aggressive whitespace removal.
Minification here happens in your browser, so a real payload is not transmitted.
How to use the JSON Minify
- Paste the formatted JSON.
- Minify, and note the before and after sizes.
- Copy the single-line result.
- If the destination is an HTTP response, make sure gzip or Brotli is enabled too - that will save considerably more than minification alone.
Examples
-
Pretty input
{ "ok": true }
JSON Minify in code
The same operation this tool performs, in the languages you are most likely to need it.
// Minify: no third argument means no whitespace
const min = JSON.stringify(JSON.parse(raw));
console.log(raw.length, "->", min.length,
`(${Math.round((1 - min.length / raw.length) * 100)}% smaller)`);
// Careful: this is a re-serialisation, not a text transform. It will
// normalise number formatting (1.0 -> 1, 1e3 -> 1000) and drop any
// key order guarantees your consumer might have relied on.
import json
# The default separators leave a space after each comma and colon -
# you must pass separators explicitly to get true minification.
minified = json.dumps(json.loads(raw), separators=(",", ":"))
# ensure_ascii=True (the default) escapes non-ASCII as \uXXXX, which
# makes the output LARGER. Pass False if the transport is UTF-8 safe.
minified = json.dumps(json.loads(raw), separators=(",", ":"),
ensure_ascii=False)
# jq
jq -c . file.json > file.min.json
# Compare what minification saves against what gzip saves
wc -c file.json
jq -c . file.json | wc -c
gzip -9 -c file.json | wc -c # usually much smaller than both
When you need this
- Fitting a JSON value into an environment variable, cookie or query parameter.
- Storing a payload in a database column where size affects row and index size.
- Reducing the size of a message on a queue with a per-message limit.
- Making a payload a single line so it fits one log entry.
- Embedding JSON in a shell command or CI configuration without newline trouble.
Common problems and what causes them
- Python's json.dumps not actually minifying
- The default separators are (", ", ": ") - with spaces. Without separators=(",", ":") you get a document that is barely smaller than the input.
- ensure_ascii making the output bigger
- Python escapes non-ASCII characters as \uXXXX by default, so a document full of accented text or CJK grows substantially. Pass ensure_ascii=False when the transport handles UTF-8.
- Expecting minification to replace compression
- Gzip and Brotli compress repeated whitespace almost to nothing, so minifying an already-compressed HTTP response saves far less than the uncompressed comparison suggests. Enable compression first; it is the larger win by a wide margin.
- Minifying inside a string value
- If a field contains JSON as an escaped string, minifying the outer document leaves the inner string untouched - the whitespace inside it is data, not formatting. It has to be parsed and re-serialised separately.
- Number formatting changing
- Minification via parse-then-stringify re-serialises numbers: 1.0 becomes 1, 1e3 becomes 1000, and integers above 2^53 lose precision. If the exact text matters, this is not a safe transform.
FAQ
- Does minifying JSON change the data?
- The parsed value is identical, because whitespace between tokens is not significant. What can change is the exact text of numbers - 1.0 serialises as 1 - and, for integers above 2^53, precision is lost in the round trip.
- How much smaller does JSON get?
- Typically 15-30% for pretty-printed input, depending on nesting depth and indentation width. Deeply nested documents with two-space indentation gain the most; a flat document with long string values gains very little.
- Should I minify JSON API responses?
- It is free, so yes - but enable gzip or Brotli first, because that saves considerably more. Once compression is on, minification is a marginal extra gain rather than the main lever.
- What actually makes a large JSON payload big?
- In an array of many objects, the repeated key names usually dominate - far more than whitespace. If size is a genuine constraint at that point, restructure to a columnar shape with keys listed once and values in parallel arrays.
- Will it change numbers?
- The minifier parses and re-serializes JSON; extremely large integers may lose precision as in standard JSON.parse.
Related reading
- How to pretty-print JSON
- Common JSON validation errors
- JSON formatter the other direction