This post gathers JSON’s syntax rules in one place. A quick reference to check whenever a question comes up, without needing to reread longer posts about each specific topic.
Overall structure
A JSON document is always a single value at the top, usually an object or array (though technically an isolated number, string, boolean, or null is also valid JSON).
{"name": "Ana"}
["item1", "item2"]
Keys (in objects)
- Always in double quotes
- Never single quotes, never unquoted
{"name": "Ana"}
Strings
- Always in double quotes
- Special characters need to be escaped:
\",\\,\/,\n,\t,\r
"line 1\nline 2"
Numbers
- No quotes
- No leading zero (
01is invalid) - Decimal point needs a digit before and after (
.5and5.are invalid) - Scientific notation is accepted (
1.5e10)
42
-7
3.14
1.5e10
Booleans
- Only
trueorfalse, no quotes, always lowercase
true
Null
- Only
null, no quotes, always lowercase
null
Commas
- Separate items inside objects and arrays
- There should never be a comma after the last item (trailing comma)
{"a": 1, "b": 2}
What is NOT allowed in JSON
- Comments (
//or/* */) - Trailing comma
- Single quotes
- Unquoted keys
undefined,NaN,Infinity- Functions
Nesting
Objects and arrays can be nested freely, in any combination and at any depth.
{
"name": "Ana",
"addresses": [
{ "type": "home", "city": "New York" },
{ "type": "work", "city": "Boston" }
]
}
Whitespace
Spaces, tabs, and line breaks between elements are ignored by the parser and only serve for readable formatting. These two examples are equivalent:
{"name":"Ana","age":30}
{
"name": "Ana",
"age": 30
}
Encoding
JSON should be encoded in UTF-8 by default (the spec also allows UTF-16 and UTF-32, but UTF-8 is by far the most common in practice).
Quick syntax checklist
- Keys and strings in double quotes?
- No trailing comma at the end of objects/arrays?
- No comments?
- Numbers with no leading zero and with digits before/after the decimal point?
true,false,nullalways lowercase, no quotes?
To quickly validate whether a JSON follows all these rules, paste it into JSON Validator.