AdvertisementLeaderboard · 728 × 90

JSON Syntax Rules Explained (With Valid and Invalid Examples)

JSON's entire grammar fits on one page — that is its genius. But the rules are strict, and several differ from JavaScript in ways that generate endless parse errors. Here is the complete rulebook, each rule with a valid and invalid example.

The six data types

A JSON value is exactly one of: object {}, array [], string, number, boolean (true/false), or null. Nothing else exists — no dates, no undefined, no functions, no comments.

Objects

✓ {"name": "Amina", "age": 34}
✗ {name: "Amina"}        // keys must be double-quoted strings
✗ {"name": "Amina",}     // no trailing comma
✗ {"a": 1 "b": 2}        // commas required between pairs

Duplicate keys are technically not forbidden by the grammar, but behaviour is undefined — most parsers keep the last value silently. Treat duplicates as errors.

Arrays

✓ [1, "two", true, null, {"x": 3}]   // mixed types are fine
✗ [1, 2, 3,]                          // no trailing comma
✗ [1 2 3]                             // commas required

Strings

✓ "It said \"hello\" twice"
✓ "Line one\nLine two"
✓ "caf\u00e9"
✗ 'single quotes'
✗ "an actual
newline inside"

Strings use double quotes only. The legal escapes are \" \\ \/ \b \f \n \r \t and \uXXXX. A raw line break inside a string is invalid — it must be written \n.

Numbers

✓ 42    ✓ -3.14    ✓ 2.5e10    ✓ 0.5
✗ .5          // must have a leading digit
✗ 05          // no leading zeros
✗ 0x1A        // no hex
✗ NaN  ✗ Infinity   // not JSON values

One practical warning: JSON places no limit on number size, but JavaScript parses numbers as 64-bit floats, so integers beyond 253 silently lose precision. APIs therefore send big IDs as strings — if you have ever seen "id": "9007199254740993", that is why.

Booleans and null

✓ true  ✓ false  ✓ null
✗ True  ✗ FALSE  ✗ None  ✗ undefined

All lowercase, exactly. True and None are the classic tells of a Python dict printed with str() instead of serialised with json.dumps().

Whitespace and nesting

Whitespace between tokens is insignificant — which is why the same document can exist minified or pretty-printed with identical meaning. Values nest to any depth: objects in arrays in objects, without limit in the grammar (parsers impose practical depth limits).

The document itself

Per the current standard (RFC 8259), a JSON document may be any value — "hello" alone is a legal document. Older parsers required an object or array at the top level, so for maximum interoperability most APIs still wrap everything in one. Encoding is UTF-8, without a byte-order mark.

Suspect a snippet breaks one of these rules? Paste it into our JSON validator — it reports the first violation with its line number, entirely in your browser.

AdvertisementIn-content · responsive
→ Test any snippet against these rules — free JSON validatorFree · 100% client-side · no signup
AdvertisementAnchor · 728×90 / 320×50