JSON looks simple until a trailing comma or an unquoted key breaks a build. This guide covers the rules that actually cause failures, how to read parser errors, and when formatting or minifying is useful.
The strict grammar
JSON is a data interchange format, not JavaScript. Several conveniences that are valid in JavaScript objects are invalid JSON:
- Trailing commas:
{"a": 1,} - Comments:
// noteor/* note */ - Unquoted keys:
{a: 1} - Single-quoted strings:
{'a': 1} undefined,NaNandInfinity- Functions or expressions
If a payload contains any of these, a strict parser rejects the whole document. That strictness is the point: every JSON parser in every language agrees on what the document means.
Reading a parse error
Modern parsers report a position. A message like “Unexpected token } in JSON at position 42” tells you where parsing stopped, not necessarily where the mistake began. The real cause is often just before that position: a missing comma, an extra comma, or an unclosed quote.
A reliable workflow:
- Jump to the reported line.
- Look at the token before the error position.
- Check the matching brackets and quotes around it.
- Fix one error and re-run; parsers usually stop at the first problem.
Formatting versus minifying
Formatting adds indentation and line breaks. It does not change values, key order or types. It makes diffs readable and review possible.
Minifying removes insignificant whitespace. It makes the payload smaller for transport, but it also makes review harder. A sensible rule is to format while working and minify only for delivery, keeping the original somewhere version-controlled.
Neither operation should reorder object keys. If you need stable ordering for hashing or signing, sort keys explicitly with a dedicated tool or in code.
Duplicate keys
The JSON specification does not give duplicate keys a meaningful interpretation. Most parsers keep the last value. That means a document with {"a": 1, "a": 2} may be accepted and silently become {"a": 2}. If exact duplication matters, detect it before parsing with a linter.
Encoding and Unicode
JSON is Unicode text, normally encoded as UTF-8. Escapes such as \u00e9 and literal é are equivalent. When a formatter re-serializes a document, it may choose one representation; the parsed value is the same.
Watch out for:
- Byte order marks at the start of a file, which some parsers reject.
- Smart quotes pasted from word processors.
- Invisible characters introduced by copy/paste.
Size and performance
Very large JSON files are better handled with streaming parsers than with a browser text area. For everyday configuration files and API responses under a megabyte, a local formatter is faster than round-tripping through a server and avoids sending potentially sensitive payloads anywhere.
Paste your document into the JSON formatter to validate it, or use the text toolkit if the problem is whitespace rather than syntax.