JSON Syntax
JSON (JavaScript Object Notation) has a minimal, strict syntax. It supports six value types, two structural types (objects and arrays), and explicit rules about whitespace, quoting, and commas. This guide covers every rule with examples.
JSON values
Any JSON document is a single value. That value is one of six types:
"a string" // string
42 // number
3.14 // number (decimal)
true // boolean
false // boolean
null // null
{"key": "value"} // object
[1, 2, 3] // arrayStrings
JSON strings must be enclosed in double quotes. Single quotes are not valid.
// Valid
"hello"
"hello world"
"it's fine" // single quote inside is OK
"say \"hi\"" // escaped double quote inside
// Invalid
'hello' // single quotes not allowed
hello // unquoted stringSpecial characters that must be escaped inside a JSON string:
| Character | Escape sequence |
|---|---|
| Double quote | \" |
| Backslash | \\ |
| Newline | \n |
| Carriage return | \r |
| Tab | \t |
| Unicode code point | \uXXXX |
Numbers
JSON numbers are written without quotes. They can be integers, decimals, or use scientific notation:
42 // integer
-7 // negative integer
3.14 // decimal
1.5e10 // scientific notation
-2.1E-3 // negative scientific
// Invalid
07 // leading zero (not allowed)
0xFF // hex (not allowed)
NaN // not a JSON value
Infinity // not a JSON valueBooleans and null
Boolean values are the literals true and false (lowercase only). The absence of a value is represented by null.
true
false
null
// Invalid
True // wrong case
False // wrong case
NULL // wrong case
undefined // not a JSON valueObjects
A JSON object is an unordered collection of key-value pairs enclosed in curly braces. Keys must be strings. Key-value pairs are separated by commas. The last pair must not have a trailing comma.
{
"name": "Alice",
"age": 30,
"active": true
}
// Invalid — trailing comma after last pair
{
"name": "Alice",
"age": 30, ← trailing comma not allowed
}
// Invalid — unquoted key
{
name: "Alice" ← key must be a string
}Duplicate keys are technically allowed by the spec but should be avoided — parsers handle them differently (some keep the last, some keep the first).
Arrays
A JSON array is an ordered list of values enclosed in square brackets. Values are separated by commas. Like objects, no trailing comma is allowed.
[1, 2, 3]
["a", "b", "c"]
[true, null, 42, "mixed"]
[[1, 2], [3, 4]] // nested arrays
[{"id": 1}, {"id": 2}] // array of objects
// Invalid — trailing comma
[1, 2, 3,] ← not allowedNesting
Objects and arrays can be nested to any depth:
{
"user": {
"id": 1,
"name": "Alice",
"roles": ["admin", "editor"],
"address": {
"city": "London",
"country": "UK"
}
}
}Whitespace
Whitespace (spaces, tabs, newlines) is allowed between tokens and has no semantic meaning. These two documents are identical:
// Pretty-printed (same data as below)
{
"name": "Alice",
"age": 30
}
// Minified (same data as above)
{"name":"Alice","age":30}Common JSON syntax errors
| Error | Invalid | Fixed |
|---|---|---|
| Trailing comma | {"a":1,} | {"a":1} |
| Single-quoted string | {'key':'val'} | {"key":"val"} |
| Unquoted key | {key:"val"} | {"key":"val"} |
| JavaScript comment | {"a":1 // note} | Remove comment |
| Missing comma | {"a":1 "b":2} | {"a":1,"b":2} |
| Undefined value | {"a":undefined} | Use null |
Validate JSON syntax online
Paste your JSON into the JSON Validator to check for syntax errors. The validator reports the exact line and column of each error. Use the fix invalid JSON guide for step-by-step repair instructions.