Learn/JSON Fundamentals

JSON Syntax Rules

JSON has a small, strict set of syntax rules. Understanding them prevents the most common parsing errors and makes you efficient at reading and writing JSON by hand.

Beginner~10 min read

The Five Core Rules

1

Double Quotes Only

All strings and keys must use double quotes (")

2

No Trailing Commas

The last item in an object/array must not have a comma after it

3

No Comments

Standard JSON does not support // or /* */ comments

4

UTF-8 Encoding

JSON documents must be encoded in UTF-8

5

Single Root Value

A JSON document must have exactly one root value (object, array, or primitive)

Strings & Quoting Rules

Every string in JSON — both keys and values — must be enclosed in double quotes. This is the most common source of errors for developers coming from JavaScript or Python.

Valid vs Invalid Quoting

Valid

{ "name": "John" }
{ "age": 30 }
{ "items": ["a", "b"] }

Invalid

{ 'name': 'John' } // single quotes
{ name: "John" } // unquoted key
{ "name": John } // unquoted value

Escape Sequences

Special characters inside strings must be escaped with a backslash:

SequenceCharacterExample
\"Double quote"He said \"hello\""
\\Backslash"C:\\Users\\file"
\nNewline"Line 1\nLine 2"
\tTab"Col1\tCol2"
\rCarriage return"Windows\r\n"
\/Forward slash"path\/to\/file"
\bBackspaceRarely used
\fForm feedRarely used
\uXXXXUnicode"Heart: \u2764"

Commas & Separators

Commas separate items in objects and arrays. The rules are simple but strict:

Correct comma usagejson
1{
2 "name": "Alice",
3 "age": 30,
4 "hobbies": ["reading", "coding", "hiking"]
5}

No Trailing Commas

Unlike JavaScript, JSON does not allow a comma after the last item.{"a": 1, "b": 2,} ← INVALID (trailing comma)

Whitespace & Formatting

Whitespace between JSON tokens is insignificant. These two documents are identical:

Pretty-printedjson
{
"name": "Alice",
"age": 30
}
Minified (identical data)json
{"name":"Alice","age":30}

Use pretty-printed JSON for readability during development, and minified JSON for production APIs and data transfer to reduce bandwidth.

Top 10 Syntax Mistakes

1

Using single quotes

Always use double quotes for strings and keys

2

Trailing comma after last item

Remove the comma after the final element

3

Adding comments (// or /* */)

JSON does not support comments

4

Unquoted keys

All object keys must be double-quoted strings

5

Using undefined

Use null instead — undefined is not valid JSON

6

Leading zeros on numbers

007 is invalid; use 7 instead

7

Literal newlines in strings

Use \n escape sequence instead

8

Using NaN or Infinity

These are not valid JSON values — use null or a string

9

Unescaped backslashes

Use \\ to represent a literal backslash

10

Multiple root values

A JSON document must have exactly one root value

Try It Yourself

This JSON has three deliberate errors. Can you fix them all? Click Run & Validate to check.

Try It Yourself

Fix the three syntax errors to make this valid JSON

Frequently Asked Questions

Can JSON have trailing commas?
No. Trailing commas after the last element in an object or array are not allowed in standard JSON. This is one of the most common syntax errors.
Can JSON have comments?
Standard JSON (RFC 8259) does not support comments. Use JSONC or JSON5 if you need comments, or use a separate documentation file.
Is whitespace significant in JSON?
No. Whitespace (spaces, tabs, newlines) between tokens is ignored by JSON parsers. You can minify JSON or pretty-print it without changing the data.
What encoding does JSON use?
JSON must be encoded in UTF-8 (RFC 8259). Earlier specs allowed UTF-16 and UTF-32, but the current standard requires UTF-8 for interoperability.
Is there a maximum nesting depth for JSON?
The JSON specification does not define a maximum depth. However, parsers typically have implementation limits (commonly 100-1000 levels) to prevent stack overflow attacks.