JSON Syntax
JSON has one of the smallest grammars of any data format. Once you know the handful of rules on this page, you can read and write any JSON document.
The core rules
- Data is written as name/value pairs:
"name": value. - Pairs are separated by commas.
- Curly braces
{ }hold objects. - Square brackets
[ ]hold arrays. - Names (keys) are always strings in double quotes.
- Strings use double quotes only — never single quotes.
{
"id": 7,
"username": "linh_dev",
"skills": ["HTML", "CSS", "JSON"],
"verified": false
}Valid JSON: object with 4 keys
{
"id": 7,
"username": "linh_dev",
"skills": [
"HTML",
"CSS",
"JSON"
],
"verified": false
}
Name/value pairs
A pair is a key in double quotes, a colon, and a value. Whitespace around the colon is optional:
"country": "Vietnam"In JavaScript you could write {country: "Vietnam"} without quotes around the key. JSON does not allow that — the key must be quoted:
{country: "Vietnam"}Invalid JSON: Expected property name or '}' in JSON at position 1 (line 1 column 2)
The six kinds of values
A JSON value must be one of these:
| Type | Example |
|---|---|
| string | "hello" |
| number | 42, -3.5, 1.2e3 |
| boolean | true, false |
| null | null |
| object | {"a": 1} |
| array | [1, 2, 3] |
Nothing else is allowed: no functions, no undefined, no dates, no comments, no NaN.
Whitespace
Spaces, tabs, and line breaks between tokens are ignored, so these two documents are identical in meaning:
{"a":1,"b":[true,null]}{
"a": 1,
"b": [ true, null ]
}Whitespace inside a string, however, is part of the value: "a b" and "ab" are different strings.
A document can be any value
A JSON document doesn't have to be an object. Any single value is a valid JSON text — a lone number, a string, or an array:
["red", "green", "blue"]Valid JSON: array with 3 items
[
"red",
"green",
"blue"
]
"just a string"Valid JSON: string
"just a string"
In practice, most APIs send an object at the top level because it is easy to add new fields later without breaking clients.
Commas: between items, never after the last
Commas separate items. A comma after the final item — a trailing comma — is an error in JSON, even though JavaScript allows it:
{
"first": 1,
"second": 2,
}Invalid JSON: Expected double-quoted property name in JSON at position 31 (line 4 column 1)
Remove the last comma and run it again.
JSON vs JavaScript object literals
JSON looks like JavaScript, but it is stricter:
| Feature | JavaScript literal | JSON |
|---|---|---|
Unquoted keys {a: 1} |
✅ | ❌ |
Single quotes 'text' |
✅ | ❌ |
| Trailing commas | ✅ | ❌ |
| Comments | ✅ | ❌ |
undefined, functions |
✅ | ❌ |
Hex numbers 0xFF |
✅ | ❌ |
Note: Because JSON is a (near) subset of JavaScript, a JSON document is also a valid JavaScript expression. The reverse is not true.
Files and encoding
JSON files use the .json extension and should be encoded as UTF-8. You can write any Unicode character directly in a string — "Đà Nẵng" is perfectly valid.
Test yourself
Which of these is a valid JSON object?
- {name: "An"}
- {'name': 'An'}
- {"name": "An"}
- {"name": "An",}
Which characters surround a JSON array?
- ( )
- { }
- < >
- [ ]
Is 42 on its own a valid JSON document?
- Yes, any single value is a valid JSON text
- No, the top level must be an object
- No, the top level must be an array
- Only if it is quoted