JSON reference
JSON syntax and data type reference
Review strict JSON objects, arrays, strings, numbers, booleans, null values, escaping rules, and common differences from JavaScript object literals.
Document summary
A compact technical reference for writing and validating strict JSON that can be opened in standards-compliant parsers.
Key takeaways
- Property names and strings use double quotes.
- JSON supports objects, arrays, strings, numbers, true, false, and null.
- Comments, trailing commas, undefined, NaN, and Infinity are not strict JSON.
JSON has a small set of value types
| Type | Example | Important rule |
|---|---|---|
| Object | {"name":"Ava"} | Keys are double-quoted strings |
| Array | [1,2,3] | Values are ordered and separated by commas |
| String | "hello" | Double quotes and control characters must be escaped |
| Number | 12.5 | No NaN, Infinity, or leading plus sign |
| Boolean | true | Lowercase true or false |
| Null | null | Lowercase null |
Objects and arrays define structure
An object contains name-value members inside braces. An array contains ordered values inside brackets. Values can be nested to any practical depth supported by the parser and available memory.
Commas appear between members or elements, never after the final item in strict JSON.
{
name: "Ava",
"roles": ["editor",],
}{
"name": "Ava",
"roles": ["editor"]
}Strings require double quotes and valid escapes
JSON strings use double quotes. A quotation mark, backslash, or control character inside the string must be escaped. Unicode characters may appear directly when the encoding supports them or as escape sequences.
| Character | JSON escape |
|---|---|
| Quotation mark | \" |
| Backslash | \\ |
| New line | \n |
| Carriage return | \r |
| Tab | \t |
| Unicode code unit | \uXXXX |
JSON numbers use a restricted decimal grammar
JSON numbers may include a minus sign, integer digits, a fractional part, and an exponent. Leading zeros are not allowed except for zero itself.
Large integers and precise decimals may lose precision in some programming languages. Use strings when exact identifiers or arbitrary precision values must be preserved.
- Valid: 0, -12, 3.14, 6.02e23
- Invalid: +1, 01, .5, NaN, Infinity
- Review precision after parsing in JavaScript and other floating-point environments.
JSON is not a JavaScript object literal
JavaScript object literals can support comments, single quotes, unquoted identifiers, methods, undefined, and other language features. Strict JSON supports none of those additions.
Use JSON.parse for strict JSON and avoid evaluating JSON text as executable code.
Validate before using JSON
- 1
Confirm the document has one complete top-level value.
- 2
Use double quotes for keys and strings.
- 3
Remove comments and trailing commas.
- 4
Check escape sequences.
- 5
Review number precision and data types.
- 6
Validate against JSON Schema when the document is an API or data contract.