Valid JSON is stricter than a JavaScript object
Most JSON errors come from the same place: the text is a perfectly good JavaScript object literal, and JSON is a narrower format than that.
JSON allows exactly six value types — string, number, object, array, boolean and null. Strings and keys must use double quotes. There are no comments, no trailing commas, no unquoted keys, no single quotes, no functions, no undefined, and no NaN or Infinity.
If your editor accepts something the formatter rejects, this is almost always why.
Common JSON errors and what causes them
The parser reports where it gave up, which is usually one character after the real mistake.
Frequent JSON parse errors and their causes
| Error |
Usual cause |
Fix |
| Unexpected token } |
A trailing comma before the closing brace |
Remove the comma after the last pair |
| Unexpected token ' |
Single-quoted strings or keys |
Change every quote to a double quote |
| Unexpected token in JSON at position 0 |
The content is not JSON at all — often an HTML error page returned by an API |
Check the response status and content type |
| Unexpected end of JSON input |
The payload was truncated, or a bracket is unclosed |
Check that every { and [ has a partner |
| Unexpected token N |
A literal NaN, Infinity or undefined |
Use null, or a number |
| Bad control character in string literal |
A raw newline or tab inside a string |
Escape it as \n or \t |
Sorting keys to compare two payloads
JSON objects have no defined key order, so two responses that are semantically identical can serialise their fields in different orders and produce a diff full of noise.
Formatting both with Sort keys alphabetically ticked, and the same indentation, normalises them: a diff of the two outputs then shows only genuine differences. This is the fastest way to answer "what actually changed between these two API responses?"
When to minify
Minified JSON — no spaces, no line breaks — is what you want on the wire and in storage: it is smaller, and machines do not need the indentation. Formatted JSON is for humans reading it.
In practice, format while you are debugging and minify before you paste a payload into a config value, a request body or an environment variable. Note that gzip, which almost every server applies, already removes most of the cost of whitespace over HTTP, so minifying JSON is more about tidiness in fixed-size fields than about bandwidth.