JSON Syntax Rules Explained (With Examples)
9 min read
JSON has a deliberately tiny grammar, but it is strict β a single misplaced comma will make a parser reject the entire document. This guide covers every rule you need, with examples of what is valid and what is not, so your data parses the first time.
1. The whole document is one value
A valid JSON document contains exactly one top-level value. That value is usually an object or an array, but it can also be a single string, number, boolean, or null.
{ "ok": true } // valid: top-level object
[1, 2, 3] // valid: top-level array
"just a string" // valid
42 // valid
true // validWhat you cannot do is place two values side by side at the top level. Two objects with nothing between them is invalid β that is a frequent mistake when concatenating API responses. If you need many records in one file, wrap them in an array, or use newline-delimited JSON (one object per line, parsed line by line).
2. Keys must be double-quoted strings
Every object key has to be a string wrapped in double quotes. Single quotes and unquoted keys are not allowed, even though JavaScript permits them.
{ "name": "Ada" } // valid
{ 'name': 'Ada' } // INVALID - single quotes
{ name: "Ada" } // INVALID - unquoted keyDuplicate keys are technically not forbidden by the grammar, but the result is undefined β most parsers keep the last one. Treat duplicate keys as a bug.
3. Strings use double quotes too
String values follow the same rule as keys: double quotes only. Inside a string, certain characters must be escaped with a backslash: double quote (\"), backslash (\\), and control characters such as newline (\n), carriage return (\r) and tab (\t).
{ "quote": "She said \"hi\"" } // valid
{ "path": "C:\\Users\\Ada" } // valid - escaped backslashes
{ "line": "first\nsecond" } // valid - escaped newlineYou can also include any Unicode character directly, or use a \u escape followed by four hex digits, e.g. "Γ©" for Γ©. Both forms are valid.
4. No trailing commas
This is the single most common mistake. You may separate items with commas, but you must not place a comma after the last item in an object or array.
{ "a": 1, "b": 2 } // valid
{ "a": 1, "b": 2, } // INVALID - trailing comma
[1, 2, 3] // valid
[1, 2, 3,] // INVALID - trailing commaThe reverse mistake β a missing comma between two items β is equally fatal. Every pair of adjacent items needs exactly one comma between them.
5. Numbers are plain decimal
Numbers can be integers or decimals and may use exponent notation. They cannot have leading zeros, a leading +, hex notation, or values like NaN and Infinity.
42 // valid
-3.14 // valid
2.5e10 // valid
0.5 // valid
42 // valid
0042 // INVALID - leading zero
+5 // INVALID - leading plus
.5 // INVALID - must be 0.5
5. // INVALID - needs a digit after the dot
NaN // INVALID
0xFF // INVALID - hex not allowedRemember that all JSON numbers are conceptually double-precision floats. Integers larger than 2^53 may lose precision when parsed, which is why large IDs are usually sent as strings.
6. Only these value types
A value must be a string, number, object, array, true, false, or null. Things that are valid in JavaScript but not in JSON include undefined, functions, comments, dates as objects, and special numeric values (dates are stored as strings).
{ "value": undefined } // INVALID
{ "value": null } // valid
{ "fn": function(){} } // INVALID
// comments are NOT allowed in JSON7. Whitespace is mostly ignored
Spaces, tabs and line breaks between tokens are ignored, which is why a document can be minified to one line or pretty-printed across many lines without changing its meaning. You can switch between those two forms instantly in the JSON Wallet editor with the Format and Minify buttons. Whitespace inside a string, however, is significant β a space in "a b" is part of the value.
8. Encoding is UTF-8
JSON text should be encoded as UTF-8. A stray byte-order mark (BOM) at the start of a file, or "smart quotes" pasted from a word processor (β β instead of " "), will break parsing even though the text looks correct. When in doubt, save as plain UTF-8 without a BOM.
Quick checklist
- Double quotes around every key and every string value.
- Exactly one comma between items, and none after the last.
- No comments, no
undefined, no functions. - Only the seven allowed value types.
- Escape special characters inside strings.
- Plain decimal numbers, no leading zeros or hex.
- UTF-8, no BOM, no smart quotes.
Frequently asked questions
Can JSON have comments?
No. Standard JSON does not allow // or /* */ comments. Supersets like JSONC (used by VS Code) and JSON5 do, but a standard parser will reject them. If you need to annotate config, add a string field such as "_comment".
Why does my JSON fail with a trailing comma when my code editor accepts it?
JavaScript and many editors tolerate trailing commas; JSON does not. The strict grammar is intentional so that every parser behaves identically. Remove the comma after the last element.
Are JSON keys case-sensitive?
Yes. "Name" and "name" are different keys. Most APIs use a consistent convention (often camelCase or snake_case) β mixing them is a common source of bugs.
If a document breaks one of these rules, see our guide on common JSON errors and how to fix them, or paste it into the editor to see the exact line and column of the first problem.
Keep reading
What Is JSON? A Beginnerβs Guide
JSON is the most widely used data format on the web. This guide explains what it is, how it is structured, and where you will run into it as a developer.
Common JSON Errors and How to Fix Them
Every developer hits "Unexpected token in JSON" eventually. Here are the most common JSON errors, what causes them, and exactly how to fix each one.
JSON vs XML vs YAML: A Practical Comparison
JSON, XML and YAML all store structured data, but each suits different jobs. See the same data in all three and learn when to reach for which.
Ready to put this into practice? Open the free JSON editor β