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 in five rules

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:

  1. A JSON document is one value: an object, array, string, number, true, false or null.
  2. Objects are { } with "key": value pairs separated by commas. Keys are always double-quoted strings.
  3. Arrays are [ ] with values separated by commas. Values may be of mixed types.
  4. Strings use double quotes only, and control characters inside them must be escaped.
  5. No comments, no trailing commas, and whitespace between tokens is ignored.

Data types

TypeExampleNotes
String"hello"Double quotes only; Unicode allowed
Number42, -3.14, 1.5e10No leading zeros, no + sign, no NaN, Infinity or hex
Booleantrue, falseLowercase
NullnullLowercase; there is no undefined
Object{"id": 1, "tags": []}Keys must be double-quoted strings
Array[1, "two", null]Values may mix types

A complete example

{
  "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}"}.

String escapes

EscapeMeaning
\"Double quote
\\Backslash
\n, \r, \tNewline, carriage return, tab
\b, \fBackspace, 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.

Not allowed in JSON

Common parse errors and fixes

Messages below are from Chrome and Node.js (JSON.parse); Firefox and Safari word them differently but point at the same position.

InputError messageFix
{"a":1,}Expected double-quoted property nameRemove the trailing comma
{'a':1} or {a:1}Expected property name or '}'Double-quote the key
{"a":1Expected ',' or '}' after property valueClose the object
[1,2,]Unexpected token ']'Remove the trailing comma
{"a":NaN}Unexpected token 'N'Use null or a string
{"a":01}Unexpected numberDrop the leading zero or quote it
a raw line break in a stringBad control character in string literalWrite \n
{"a":1}{"b":2}Unexpected non-whitespace character after JSONWrap in an array or parse line by line
empty stringUnexpected end of JSON inputCheck the HTTP response body is not empty

JSON in code

// 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)

Command line: jq

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.

JSONPath quick reference

$.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.

Frequently asked questions

What MIME type and file extension does JSON use?

The media type is application/json and the extension is .json. JSON text must be UTF-8 when exchanged between systems (RFC 8259).

Does key order matter?

No. Objects are unordered by specification, although most parsers keep the order they read. Use the JSON sorter to sort keys for stable diffs.

Can a JSON document be just a string or number?

Yes. Since RFC 7159, any value is a valid JSON text, so "hello" and 42 are complete documents.

Are duplicate keys allowed?

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.

How do I put a comment in a JSON file?

You cannot in strict JSON. Use a "_comment" key, or switch to JSONC or JSON5 if the tool reading the file supports them.

What is the maximum size or nesting depth?

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.