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 / Syntax | Meaning | Example |
|---|---|---|
| string | Text value wrapped in double quotes | "name": "Tom" |
| number | Integer or float (no leading zero, no NaN/Infinity) | "age": 30 |
| boolean | Boolean true or false | "active": true |
| null | Empty value (must be lowercase) | "deleted": null |
| object | Unordered key-value collection in braces | {"id": 1, "name": "Tom"} |
| array | Ordered 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 \t | Control characters use escape sequences | "s": "line1\nline2" |
| \uXXXX | Unicode code point as \uXXXX | "s": "\u2728" |
| , | Members/elements separated by commas | {"a": 1, "b": 2} |
| no trailing comma | No trailing comma after the last item | ✗ {"a": 1,} |
| no comments | Standard JSON has no // or /* */ comments | 用 JSONC / JSON5 扩展 |
| root value | Top level may be an object or array | [1, 2, 3] |
| nested object | An object value may be object/array | {"user": {"id": 1}} |
| nested array | An array may hold values of any type | [1, "a", {"x": 2}] |
| duplicate keys | Later key wins on parse (avoid duplicates) | {"a": 1, "a": 2} → 2 |
| JSON.parse() | Parse a string to an object; throws on invalid | JSON.parse('{"a": 1}') |
| JSON.stringify() | Serialize an object to a string | JSON.stringify(obj, null, 2) |
| pretty / minify | Pretty-print with indent vs minify | JSON.stringify(o, null, 2) |
| application/json | Use this MIME type for transport | Content-Type: application/json |
| JSON Schema | Declarative format to describe data structure | { "$schema": "...", "type": "object" } |
| big integer | Integers beyond 2^53 lose precision in JS | "id": 9007199254740993 |
| dates | No date type; use an ISO 8601 string | "ts": "2026-08-29T00:00:00Z" |
| no leading zero | No leading zeros on numbers (e.g. 01) | "n": 1 |
| JSON vs YAML | YAML allows comments/unquoted/indent; JSON stricter, faster | — |
| JSON vs XML | JSON 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.