JSON Cheat Sheet
JSON syntax on one page: data types, escapes, forbidden syntax, error messages and the commands you use every day. Filter the quick-reference list below.
JSON syntax on one page: data types, escapes, forbidden syntax, error messages and the commands you use every day. Filter the quick-reference list below.
This JSON cheat sheet covers everything in the specification (RFC 8259) plus the errors and commands you meet in practice. The whole grammar fits in five rules:
true, false or null.{ } with "key": value pairs separated by commas. Keys are always double-quoted strings.[ ] with values separated by commas. Values may be of mixed types.| Type | Example | Notes |
|---|---|---|
| String | "hello" | Double quotes only; Unicode allowed |
| Number | 42, -3.14, 1.5e10 | No leading zeros, no + sign, no NaN, Infinity or hex |
| Boolean | true, false | Lowercase |
| Null | null | Lowercase; there is no undefined |
| Object | {"id": 1, "tags": []} | Keys must be double-quoted strings |
| Array | [1, "two", null] | Values may mix types |
{
"id": 1024,
"name": "Ada Lovelace",
"active": true,
"manager": null,
"skills": ["math", "poetry"],
"address": { "city": "London", "zip": "W1" },
"createdAt": "2026-10-09T12:00:00Z"
}
Dates have no JSON type; the convention is an ISO 8601 string as in createdAt. To nest a JSON document inside another as text (a "JSON in JSON" field), it must be escaped as a string: {"payload": "{\"id\":1}"}.
| Escape | Meaning |
|---|---|
\" | Double quote |
\\ | Backslash |
\n, \r, \t | Newline, carriage return, tab |
\b, \f | Backspace, form feed |
\/ | Forward slash (optional) |
é | Any character by its UTF-16 code unit (here é); emoji need a surrogate pair such as 😀 |
Raw line breaks and tabs inside a string are invalid and must be escaped. Single quotes need no escape inside a double-quoted string: "It's fine". The JSON escape tool does the escaping for you.
// or /* */): see JSON with comments for JSONC and JSON5.[1, 2,] or {"a": 1,}, see trailing comma errors.{'a': 1}, {a: 1}. See single quotes in JSON and unquoted keys.undefined, functions, dates and NaN.{"a":1}{"b":2}. One object per line is a different format, JSON Lines.Messages below are from Chrome and Node.js (JSON.parse); Firefox and Safari word them differently but point at the same position.
| Input | Error message | Fix |
|---|---|---|
{"a":1,} | Expected double-quoted property name | Remove the trailing comma |
{'a':1} or {a:1} | Expected property name or '}' | Double-quote the key |
{"a":1 | Expected ',' or '}' after property value | Close the object |
[1,2,] | Unexpected token ']' | Remove the trailing comma |
{"a":NaN} | Unexpected token 'N' | Use null or a string |
{"a":01} | Unexpected number | Drop the leading zero or quote it |
| a raw line break in a string | Bad control character in string literal | Write \n |
{"a":1}{"b":2} | Unexpected non-whitespace character after JSON | Wrap in an array or parse line by line |
| empty string | Unexpected end of JSON input | Check the HTTP response body is not empty |
// JavaScript
const obj = JSON.parse(text);
const text2 = JSON.stringify(obj, null, 2); // pretty, 2-space indent
const res = await fetch(url); const data = await res.json();
# Python
import json
obj = json.loads(text)
text2 = json.dumps(obj, indent=2, ensure_ascii=False)
with open("data.json", encoding="utf-8") as f: doc = json.load(f)
// PHP
$arr = json_decode($text, true, 512, JSON_THROW_ON_ERROR);
echo json_encode($arr, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
// Go
var v map[string]any
err := json.Unmarshal([]byte(text), &v)
jq . data.json # pretty print
jq -c . data.json # minify to one line
jq '.users[0].name' data.json # one value
jq '.users[] | select(.age > 30)' data.json
jq -r '.users[].email' data.json # raw strings, no quotes
python -m json.tool data.json # pretty print without jq
Try these against your own data in the jq playground.
$.store.book[0].title first book's title
$.store.book[*].author every author
$..price every price at any depth
$.store.book[-1:] last book
$.store.book[?(@.price < 10)] books cheaper than 10
Test expressions in the JSONPath tester.
Numbers above 253 (9,007,199,254,740,992) lose precision in JavaScript's JSON.parse; send large IDs as strings, see JSON big integer precision. When a config file needs comments or a lighter syntax, compare with the YAML cheat sheet.
The media type is application/json and the extension is .json. JSON text must be UTF-8 when exchanged between systems (RFC 8259).
No. Objects are unordered by specification, although most parsers keep the order they read. Use the JSON sorter to sort keys for stable diffs.
Yes. Since RFC 7159, any value is a valid JSON text, so "hello" and 42 are complete documents.
The specification says keys should be unique but does not forbid duplicates. Most parsers, including JavaScript and Python, silently keep the last value, so treat duplicates as a bug.
You cannot in strict JSON. Use a "_comment" key, or switch to JSONC or JSON5 if the tool reading the file supports them.
The specification sets no limit. Parsers do: Python's json module stops at about 1,000 levels of nesting, and JavaScript engines cap a single string at a few hundred megabytes, so very large files need a streaming parser.