JSON syntax: the complete rules

JSON fits on a business card: six kinds of value and a handful of rules. This page covers all of them, plus the parts the standard leaves to each parser.

3 min readUpdated MasterDev

Six kinds of value

A JSON document is one value. That value can be an object, an array, a string, a number, true or false, or null. Objects and arrays hold more values, which is how documents nest.

All six kinds in one document
{
  "id": 1042,
  "name": "Ana Torres",
  "active": true,
  "score": 4.8,
  "roles": ["admin", "editor"],
  "address": { "city": "Lima", "zip": null }
}

The current standard, RFC 8259, lets any value be the whole document, so "hello" and 42 are valid JSON. The original RFC 4627 required an object or an array; a few old parsers still do.

Objects

An object is a list of "key": value pairs inside curly braces, separated by commas. Keys are always strings in double quotes. Values can be anything, including other objects.

  • Order: the standard says object members are unordered. Most parsers keep the order of the text, but do not build logic that depends on it.
  • Duplicate keys: the standard says keys should be unique and leaves the behaviour open when they are not. JavaScript, Python and most Java libraries keep the last value; some parsers reject the document.
  • Empty object: {} is valid.

Arrays

An array is an ordered list of values inside square brackets, separated by commas. The values do not need to share a type, so [1, "two", null, {"three": 3}] is valid, although most APIs keep arrays uniform because typed languages need them to be. Order is significant, and [] is a valid empty array.

Strings and escapes

Strings are wrapped in double quotes and may contain any Unicode character, except that three groups must be escaped with a backslash: the double quote, the backslash itself, and control characters (U+0000 to U+001F, which include line breaks and tabs).

EscapeMeaning
\"double quote
\\backslash
\/slash (optional)
\n \r \tline feed, carriage return, tab
\b \fbackspace, form feed
\u00e9any character by its four-digit hex code (here, é)

Characters beyond U+FFFF, such as most emoji, are written as a surrogate pair when escaped: "\ud83d\ude00" is 😀. You can also write them directly; both forms mean the same string. More in the guide on escaping JSON.

Numbers

A number is an optional minus sign, an integer part, an optional fraction and an optional exponent: 0, -12, 3.1416, 6.02e23, 1E-9. What is not allowed: a leading +, leading zeros (01), hex (0x1F), a bare decimal point (.5, 5.), NaN and Infinity.

The standard sets no size or precision limit, but it warns that software usually reads numbers as 64-bit floats. Integers are only exact up to 253 − 1 (9,007,199,254,740,991). APIs with bigger IDs often send them twice, as a number and as a string; Twitter's id and id_str fields are the famous example. If you design the API, send large IDs and money amounts as strings.

true, false and null

Three literal words, lowercase only. True, FALSE and Null are invalid, and so are yes, no and None. There is no undefined: leave the key out instead.

Whitespace and encoding

Between tokens you may use spaces, tabs, line feeds and carriage returns, as many as you like. No other whitespace is allowed: a non-breaking space copied from a web page breaks the document.

JSON exchanged between systems must be encoded as UTF-8, and a byte order mark must not be added. Parsers may ignore one, but many reject it.

Formats that look like JSON

JavaScript objects
Allow unquoted keys, single quotes, trailing commas, functions and undefined. JSON is a strict subset of the syntax.
JSONC
JSON with comments, used by tsconfig.json and VS Code settings. Only special parsers read it.
JSON5
A superset with comments, single quotes, trailing commas and hex numbers, meant for config files written by hand.
NDJSON / JSON Lines
One complete JSON value per line, used for logs and streams. Each line is valid JSON; the file as a whole is not.

Real JSON files use the .json extension and the media type application/json.

Check a document

Paste any document into the JSON formatter to validate it against these rules. If it fails, the guide to common JSON errors covers each message.