Where a formatter earns its keep
- A minified API response arrives on one line and you need to find the field that is wrong
- Two config files have to be compared, and they differ in indentation and key order before they differ in content
- A JSON sample is going into a pull request or a README and should be valid before anyone copies it
- A log line holds a fragment of JSON and you want to know whether it was cut off or is simply oddly shaped
Re-indenting the text, not re-serialising the data
The quick way to pretty-print JSON in JavaScript is JSON.stringify(JSON.parse(text), null, 2). It works, but every number passes through a 64-bit float on the way and comes back written the way JavaScript likes, and escapes come back decoded. This tool uses JSON.parse only to check the syntax. The output is built from the original tokens, copied one by one, with new line breaks and indentation between them.
- 1Input: {"id":9007199254740993,"price":1.10,"big":1e5,"name":"caf\u00e9"}
- 2Round-tripped through JSON.parse and JSON.stringify, id becomes 9007199254740992, price becomes 1.1, big becomes 100000 and name becomes "café".
- 3Formatted here, the four values come out as 9007199254740993, 1.10, 1e5 and "caf\u00e9", character for character; only the layout changes.
- 4Because 9007199254740993 is above 2^53, the tool also flags it: the text is intact, but any program that reads it into a double will see a different number.
What “sorted keys” means here
With key sorting on, every object at every depth is sorted; arrays keep their order, since order is part of an array’s meaning. Keys are compared by UTF-16 code units, which is not dictionary order: capitals come before lower case, an underscore falls between the two, and Hangul or other non-Latin keys come after all ASCII ones.
Sorting an object with the keys zeta, Beta, alpha, _x and list gives Beta, _x, alpha, list, zeta, and inside alpha the keys 한글, b and A come out as A, b, 한글. An array such as [3,1,2] stays [3,1,2]. This is the same order you get from sorting strings with a plain comparison in JavaScript, so a diff against output from such a script lines up.
Things that look like JSON but are not
- RFC 8259 allows exactly four whitespace characters: space, tab, line feed and carriage return. A non-breaking space (U+00A0) picked up when copying from a web page is a syntax error, and the tool points to its column.
- A byte order mark at the very start of pasted text fails at line 1, column 1. The RFC says implementations must not add one to JSON sent over a network. Opening the file with the text-file button strips it on reading.
- NaN and Infinity are not JSON numbers. Python’s json.dumps writes them anyway with its default allow_nan=True, so a file produced by Python can fail here; dump with allow_nan=False to catch it at the source.
- tsconfig.json and VS Code settings are JSONC, which allows comments, and JSON5 allows single quotes and trailing commas. Neither is standard JSON, so the strict check rejects them as they are.
Duplicate keys survive formatting
The RFC only says names within an object SHOULD be unique and warns that software receiving duplicates behaves unpredictably: many parsers keep only the last value, others refuse the document, and some hand back every pair. Because this formatter copies tokens rather than rebuilding an object, it does not pick a winner. {"b":1,"a":2,"b":3} comes out with both b lines, and with sorting on they stay in their original order after a.
That is deliberate, since silently dropping one would hide the problem, but it means a tidy result is no proof the keys are unique. Read the warning shown with the result and fix the source.