How JSON validation actually works
JSON (JavaScript Object Notation) looks close enough to a JavaScript object literal that it's easy to assume they're interchangeable — they aren't. JSON is a much stricter subset, and "validating" JSON really just means running it through a parser that enforces that stricter grammar and reports exactly where it breaks.
The rules that trip people up
- Keys must be double-quoted strings.
{name: "Ada"}is invalid — it has to be{"name": "Ada"}. - No trailing commas.
[1, 2, 3,]is invalid JSON, even though it's valid JavaScript. - No single quotes. Strings must use double quotes —
'hello'isn't valid,"hello"is. - No comments. JSON has no
//or/* */syntax at all, unlike JSON5 or JavaScript. - Numbers can't have leading zeros or a leading plus.
01and+1are both invalid;1and-1are fine.
Reading a parser error
When JSON.parse rejects an input, its error message includes a character position, like Unexpected token } in JSON at position 42. That position counts characters from the very start of
the string, including whitespace and newlines — which makes it hard to eyeball in a large document. hexnook's JSON formatter converts that raw position into an actual line and column, so you can
jump straight to the problem instead of counting characters by hand.
Format vs. validate vs. minify
These three are really the same underlying operation with a different final step. Validation is just JSON.parse succeeding or throwing. Formatting is JSON.stringify with an indent
argument, which re-serializes the already-parsed value with consistent whitespace. Minifying is the same
stringify call with no indent argument at all, producing the smallest possible output — useful when you're about
to send the JSON over the wire and don't need it to be human-readable anymore.