JSON is defined by RFC 8259 (also published as ECMA-404). The grammar is small, and nearly every validation failure comes from one of these rules:
- Strings use double quotes only.
'name'is not a string, and object keys must be strings, so{name: 1}is invalid too. - No trailing commas after the last array element or object member.
- No comments of any kind: no
//,/* */or#. - Numbers are decimal. No leading zeros (
007), no leading+, no hex, no bare.5or5., and noNaNorInfinity. - Literals are lowercase:
true,false,null.True,Noneandundefinedare errors. - Control characters must be escaped. A raw tab or newline inside a
string is invalid. Write
\tand\ninstead. - Any value can be the top level.
"hello",42andnullare complete JSON documents. The older RFC 4627 required an object or array at the top level, and a few old parsers still enforce that.
Whitespace between tokens is free: spaces, tabs, line feeds and carriage returns can appear anywhere outside a string, so indentation never affects validity.