JSON Formatter
- 125
- Bytes in
- 170
- Bytes out
- +45
- Change
- 6
- Keys
- 2
- Max depth
The formatter lays JSON out for reading and minifies it for shipping, the same parse, two output styles. When the document will not parse it reports the line, the column and, where the message allows, the actual mistake rather than the parser's wording.
How it works
The text is parsed into a real value and then re-serialised. That round trip is what guarantees the output is valid: a formatter that only inserted line breaks could produce well-laid-out text that no parser will accept.
- Formatting indents with two spaces, four spaces or tabs, and puts each key and array element on its own line.
- Minifying removes every optional character, all whitespace between tokens, which is the form to send over a network.
- Sorting keys orders every object alphabetically at every depth. Array order is data and is never touched.
- Because the value is re-serialised, comments and trailing commas do not survive: neither is valid JSON, so neither can be parsed in the first place.
Key order in an object carries no meaning in JSON, so sorting changes nothing about what the document says. It makes two versions of the same document comparable in a diff, which is the usual reason to want it.
Examples
Formatting a one-line document
JSON
{"name":"Ada Lovelace","born":1815,"fields":["mathematics","computing"]}Result
Indented across 8 lines, 3 keys, max depth 2
The depth figure counts nesting levels. The object is level one and the array inside it is level two.
Minifying for the wire
JSON
A formatted 268-byte document
Result
124 bytes
Every byte saved is indentation. Over HTTP with compression enabled the real saving is smaller, since repeated spaces compress almost to nothing.
A trailing comma
JSON
{"a": 1,}Result
Invalid at line 1, column 9: trailing comma before the closing brace
Valid JavaScript, invalid JSON. The parser message alone says "Unexpected token }", which does not point at the comma that caused it.
Frequently asked questions
Why does formatting remove my comments?
Because JSON has no comments. The document is parsed into a value and written back out, and a comment was never part of that value, it stopped the parse entirely. If you need comments, you need JSON5, JSONC or YAML, all of which are different formats.
Does minifying JSON actually make my site faster?
Marginally, and less than the byte count suggests. Any server worth using compresses responses with gzip or Brotli, and whitespace compresses to almost nothing. Minify anyway, it costs nothing, but measure the compressed size before claiming a win.
Is it safe to sort the keys?
Yes. Object key order is explicitly meaningless in JSON, so a sorted document says exactly the same thing. It is genuinely useful before a diff, because it stops two equivalent documents appearing to differ purely because their keys arrived in a different order. Array order is untouched, since that is data.
Why do my large numbers change?
JavaScript parses every JSON number as a double, so an integer beyond about 9 quadrillion loses precision and comes back slightly different. This affects large database identifiers in particular. Those should be transmitted as strings, which is what most APIs that hit this problem end up doing.
Is my data uploaded?
No. Parsing and serialising both happen in the page. That matters here because JSON pasted into a formatter is very often an API response containing real customer records.