JSON Cheat Sheet

JSON cheat sheet: data types, double-quote and backslash escaping, JSON.parse/stringify, MIME type, big-integer precision and date handling — quick lookup while writing APIs or configs.

Type / SyntaxMeaningExample
stringText value wrapped in double quotes"name": "Tom"
numberInteger or float (no leading zero, no NaN/Infinity)"age": 30
booleanBoolean true or false"active": true
nullEmpty value (must be lowercase)"deleted": null
objectUnordered key-value collection in braces{"id": 1, "name": "Tom"}
arrayOrdered list of values in brackets["a", "b", "c"]
"key"Key must be a double-quoted string"key": value
escape \"Escape a double quote inside strings as \""q": "He said \"hi\""
\\Backslash itself is escaped as \\ (e.g. path C:\\temp)"p": "C:/tmp"
\b \f \n \r \tControl characters use escape sequences"s": "line1\nline2"
\uXXXXUnicode code point as \uXXXX"s": "\u2728"
,Members/elements separated by commas{"a": 1, "b": 2}
no trailing commaNo trailing comma after the last item✗ {"a": 1,}
no commentsStandard JSON has no // or /* */ comments用 JSONC / JSON5 扩展
root valueTop level may be an object or array[1, 2, 3]
nested objectAn object value may be object/array{"user": {"id": 1}}
nested arrayAn array may hold values of any type[1, "a", {"x": 2}]
duplicate keysLater key wins on parse (avoid duplicates){"a": 1, "a": 2} → 2
JSON.parse()Parse a string to an object; throws on invalidJSON.parse('{"a": 1}')
JSON.stringify()Serialize an object to a stringJSON.stringify(obj, null, 2)
pretty / minifyPretty-print with indent vs minifyJSON.stringify(o, null, 2)
application/jsonUse this MIME type for transportContent-Type: application/json
JSON SchemaDeclarative format to describe data structure{ "$schema": "...", "type": "object" }
big integerIntegers beyond 2^53 lose precision in JS"id": 9007199254740993
datesNo date type; use an ISO 8601 string"ts": "2026-08-29T00:00:00Z"
no leading zeroNo leading zeros on numbers (e.g. 01)"n": 1
JSON vs YAMLYAML allows comments/unquoted/indent; JSON stricter, faster
JSON vs XMLJSON lighter, nested; XML has tags/attrs/namespaces

Frequently Asked Questions

What is the difference between JSON and a JavaScript object?

JSON is a pure data format: keys must be double-quoted and values are limited to string/number/boolean/null/object/array — no functions, undefined, Date objects or comments. JS object literals are looser (unquoted keys, methods allowed). `JSON.parse`/`JSON.stringify` convert between them.

Why does my JSON fail to parse?

Most common causes: unquoted keys, single-quoted strings, trailing commas, comments, or numbers like 01 / NaN / Infinity. A JSON validator or formatter quickly pinpoints the offending line number.

Why do large integers (e.g. order IDs) get mangled?

JavaScript numbers are double-precision floats; integers beyond 2^53 (9007199254740992) lose precision, so trailing digits distort after parsing. Transmit such IDs as strings, or use BigInt / decimal libraries.

When should I use JSON vs YAML?

Use YAML for human-edited config and Kubernetes manifests (readable, supports comments); use JSON for API transport, logs and NoSQL storage (fast parse, broad ecosystem). They convert to each other.