How to Minify JSON (and When You Shouldn't)
Minify JSON safely by parsing and re-serialising it. See what is removed, what can change (duplicate keys, big numbers), and code for JavaScript, Python and jq.
The short answer
To minify JSON, remove all the whitespace that sits between tokens (spaces, tabs and line breaks used for indentation) while leaving everything inside strings untouched. The safest way is to parse the JSON and write it back out without indentation, which is what JSON.stringify(JSON.parse(text)) does. Paste your JSON into the JSON Formatter and click Minify & copy: the minified version goes straight to your clipboard.
What minifying JSON actually removes
JSON allows "insignificant whitespace" around brackets, braces, colons and commas. Indentation is purely for people; parsers ignore it. Minifying deletes it.
Before (121 characters):
{
"name": "Asha Rao",
"city": "Pune",
"skills": ["SQL", "Python"],
"active": true,
"note": "two spaces stay"
}
After (99 characters):
{"name":"Asha Rao","city":"Pune","skills":["SQL","Python"],"active":true,"note":"two spaces stay"}
Two things to notice:
- The space inside
"Asha Rao"and the double space inside"two spaces stay"are kept. They are part of the data. - The saving here is about 18%. The saving grows with deeper nesting and bigger indents, because every line of indentation is removed.
How to minify JSON with the Lazyblink tool
The tool runs JSON.parse and JSON.stringify in your browser, so the JSON you paste is not uploaded.
Why parse-then-stringify is safer than "remove all spaces"
A quick search-and-replace that deletes spaces and line breaks will damage your data:
"Asha Rao"becomes"AshaRao".- A string that contains a line break written as
\nis safe, but an address or note with meaningful spaces is not.
Parsing first means the tool knows exactly which characters are inside strings and which are structure. Only structural whitespace is dropped.
Side effects of parse-then-stringify you should know
Because the JSON is parsed into JavaScript values and written back out, a few things can change. We checked each of these in Node.js, which uses the same JSON functions as the browser:
| Input | Minified output | Why |
|---|---|---|
{"a":1,"a":2} | {"a":2} | Duplicate keys: the last one wins |
{"f":1.50} | {"f":1.5} | Numbers are rewritten in their shortest form |
{"e":1e3} | {"e":1000} | Exponent notation is expanded where short enough |
{"t":"caf\u00e9"} | {"t":"café"} | Unicode escapes are written as the actual character |
{"n":12345678901234567890} | {"n":12345678901234567000} | Integers above 2^53 lose precision |
For most API payloads and config files none of this matters. The big-number case does: IDs from some databases and payment systems are longer than JavaScript can hold exactly. If your data has long numeric IDs, read why large numbers change when formatting JSON before minifying, and keep such IDs as strings.
What minification will not fix
- Trailing commas.
{"a":1,}is not valid JSON. Parsing fails with "Expected double-quoted property name". Remove the comma first. - Comments. JSON has no comments.
{"a":1} // notefails with "Unexpected non-whitespace character after JSON". Strip comments, or keep the file in a format that allows them (such as JSONC for some editors' settings files). - Single quotes and unquoted keys. These are JavaScript object syntax, not JSON.
If you hit a parse error at the very start of the input, see Unexpected token in JSON at position 0.
When you should minify JSON
- Data sent over the network where you control both ends and want smaller payloads. Many servers also apply gzip or Brotli compression, which shrinks repeated whitespace very well, so measure both before assuming a big win.
- Embedding JSON in a single line, such as an environment variable, a command-line argument, a CSV cell or an HTML
data-attribute. - Storing many small records, for example one JSON object per line (the JSON Lines format), where each record must sit on one line.
- Comparing structure when whitespace differences are getting in the way. Minify both versions, or better, pretty-print both with the same indent, then use the Text Diff Checker.
When you should not minify JSON
- Config files people edit by hand, such as
package.jsonortsconfig.json. Readability matters more than a few bytes, and version-control diffs become one giant changed line. - Logs you read during debugging. Pretty-printed JSON is far easier to scan.
- Files where key order or number formatting must be preserved exactly, such as signed payloads. Re-serialising can change the bytes, which breaks a signature even if the data looks the same.
Minify JSON in code
If you minify regularly, do it where the data is produced:
- JavaScript:
JSON.stringify(JSON.parse(text)). Leave out the third argument ofJSON.stringify(or pass 0) to get no indentation. - Python:
json.dumps(json.loads(text), separators=(",", ":")). Theseparatorsargument matters: by default Python writes", "and": "with spaces. - Command line with jq:
jq -c . input.jsonprints compact output.
All three keep spaces inside strings and remove structural whitespace, just like the tool.
Checking that nothing was lost
After minifying an important file, paste the minified copy back into the formatter and pretty-print it with the same indent as the original. Then compare the two pretty-printed versions in the diff checker. Any line that differs is a real change in the data, such as a collapsed duplicate key or a reformatted number, rather than whitespace.
Quick checklist
For a refresher on JSON syntax itself, see how to format, validate and debug JSON.
Frequently asked questions
Does minifying JSON change the data?
It removes only whitespace between tokens, so values stay the same. Because the tool parses and re-serialises the JSON, duplicate keys collapse to the last value, numbers are written in their shortest form, and integers above 2^53 can lose precision.
How do I minify JSON in Python?
Use json.dumps(json.loads(text), separators=(",", ":")). Without the separators argument, Python adds a space after every comma and colon.
Can I minify JSON that has comments or trailing commas?
No. Comments and trailing commas are not valid JSON, so parsing fails. Remove them first, then minify.
How much smaller does minified JSON get?
It depends on the indentation. In the example in this guide, a 121-character pretty-printed object became 99 characters, about 18% smaller. Deeply nested files save more. Server compression such as gzip also shrinks whitespace, so measure both.
Put this guide into practice with our free online tool — no signup required.
Open tool