The mistakes that make JSON invalid
| Mistake | Wrong | Right |
|---|---|---|
| Trailing comma | {"a": 1,} | {"a": 1} |
| Single quotes | {'a': 'x'} | {"a": "x"} |
| Unquoted key | {a: 1} | {"a": 1} |
| Comment | {"a": 1} // note | {"a": 1} |
| Python or JS literal | {"a": None, "b": undefined} | {"a": null} |
| Raw line break in a string | "line one line two" | "line one\nline two" |
All of these are legal in JavaScript object literals or in JSON5, which is why they slip in. Strict JSON (RFC 8259) accepts none of them.
Format, minify and sort
- Format adds line breaks and indentation so the structure is readable.
- Minify removes all whitespace outside strings, which is what you want on the wire.
- Sort keys orders every object's keys alphabetically. Two JSON documents with the same data then become identical text, which makes them easy to compare in the text diff tool.
Formatting never changes the data. One caveat: numbers larger than 253 lose precision in any JavaScript-based tool, this one included. Keep such IDs as strings.
Questions
Why is the error position sometimes one token late?
The parser reports where it could no longer continue. With a missing comma, that is the start of the next value, not the end of the previous one. Look just before the caret.
Are duplicate keys an error?
Not for the parser. The last value wins and the earlier one disappears from the output.
How large a file can I paste?
Several megabytes work on a normal laptop. The limit is the browser tab's memory, since nothing is sent to a server.