A JSON text is one value with optional white space around it. This page covers the parts of the grammar in RFC 8259 other than strings and numbers.
Grammar
The whole grammar, in the ABNF of RFC 8259, with the strings and numbers on their own pages:
JSON-text = ws value ws
value = false / null / true / object / array / number / string
object = begin-object [ member *( value-separator member ) ] end-object
member = string name-separator value
array = begin-array [ value *( value-separator value ) ] end-array
false = %x66.61.6c.73.65 ; false
null = %x6e.75.6c.6c ; null
true = %x74.72.75.65 ; true
Any value may stand at the top level. That has been so since RFC 7159; RFC 4627 allowed only an object or an array, as the versions page explains.
White space
Four characters count as white space, and they may appear before or after any of the structural characters, and before and after the value at the top level:
| Byte | Character |
|---|---|
0x20 | Space |
0x09 | Horizontal tab |
0x0A | Line feed |
0x0D | Carriage return |
No other character is white space: not a form feed, a vertical tab, a no-break space or a byte order mark.
Structural characters
| Character | Byte | Name in the grammar |
|---|---|---|
[ | 0x5B | begin-array |
] | 0x5D | end-array |
{ | 0x7B | begin-object |
} | 0x7D | end-object |
: | 0x3A | name-separator |
, | 0x2C | value-separator |
A comma only ever separates: there is no comma after the last member or element. JSON has no comments.
Literal names
The three literal names are true, false and
null. They must be lower case, and there are no others:
True, NULL, undefined, NaN and
Infinity are not JSON.
Objects
An object is zero or more members between braces. A member is a string, called its name, then a colon, then a value. The names need not look like identifiers: any string will do, including the empty string.
{ "x": 1, "y": [true, null], "": "an empty name is allowed" }
Names should be unique
RFC 8259 says the names within an object should be unique. It does not forbid
repeats, so {"a":1,"a":2} is grammatically valid, but it warns that the
behaviour of software receiving such an object is unpredictable: some report only the
last pair, some report an error, and others report all of them. An object whose names are
all unique is the only kind every implementation agrees on. Whether two names are the same
is decided after escapes are decoded, as described under
comparing strings.
Member order
RFC 8259 calls an object an unordered collection. Parsers differ on whether they show the order of the members to the program using them, so software should not depend on it.
Arrays
An array is zero or more values between square brackets, separated by commas. The values
need not be of the same kind, so [1, "two", [3], {"four": 4}, null] is one
valid array. Unlike the members of an object, the elements of an array are ordered.