JSON Cheatsheet
In frontend-backend integration, JSON is the default format for API responses and one of the easiest to break with a trailing comma or an unquoted key. This reference covers 6 high-frequency groups — data types, objects, arrays, nesting, common API response patterns, and validation/formatting commands (JSON.parse, jq). It helps you write well-formed data from a template and gives you a checklist for debugging plus jq for quick validation. After reading you can assemble valid JSON and verify it with tooling quickly.
Data Types 6
"string"123 / -456 / 3.14true / falsenull{"key": "value"}[1, 2, 3]Object 5
{"name": "tom"}{"name": "tom", "age": 20}{"user": {"name": "tom"}}{"users": [{"name": "tom"}]}{"key with space": "value"}Array 5
[1, 2, 3]["a", "b", "c"][{"id": 1}, {"id": 2}][[1, 2], [3, 4]][1, "text", true, null]Nested 3
{"user": {"name": "tom", "age": 20}}{"users": [{"name": "tom"}, {"name": "jerry"}]}[{"id": 1, "tags": ["a", "b"]}]Common Patterns 4
{"code": 200, "message": "success"}{"error": {"code": 404, "message": "Not found"}}{"data": {"items": [], "total": 100}}{"id": 1, "created_at": "2026-07-19T10:30:00Z"}Validation 5
JSON.parse(str)JSON.stringify(obj)JSON.stringify(obj, null, 2)jsonlint file.jsonjq . file.jsonTypical Use Case
In API integration and frontend-backend handoff, JSON is the default exchange format, with three recurring scenarios: building a request payload, reading nested fields from a response, and troubleshooting why JSON.parse throws. When the API returns a paginated data.items array plus a total, you follow the comma patterns to read the fields; when building a request you must keep every key double-quoted with no trailing comma. A quick jsonlint or jq check afterwards cuts down a lot of back-and-forth during integration. After reading this table you can write well-formed JSON and validate it with tooling yourself.
Command Examples
Validate and pretty-print a JSON file
jsonlint data.json\njq . data.jsonjsonlint 语法正确时无输出,出错会报错并给出行列位置;jq . 把压缩的单行 JSON 格式化为带缩进的可读形式。
Output
jq 输出:\n{\n "name": "tom",\n "age": 20\n}Pretty-print an object as indented JSON
JSON.stringify({ name: "tom", age: 20 }, null, 2)第三个参数 2 表示缩进 2 个空格,中间的 null 是替换器占位;输出便于日志打印或比对差异。
Output
{\n "name": "tom",\n "age": 20\n}Common Pitfalls
- JSON keys must be double-quoted and trailing commas are not allowed; an extra trailing comma makes the frontend JSON.parse throw.
- JSON does not support comments or unquoted keys; use JSONC or keep notes outside the file when comments are needed.
- Very large integers can lose precision when parsed in JS; represent big integers as strings when crossing systems.
- There is no native date type; standardize on ISO 8601 strings instead of ambiguous epoch integers.
- Using JSON.parse(JSON.stringify(obj)) to deep-clone drops Date, undefined and functions — only safe for plain data objects.
Tips
- JSON keys must use double quotes — single quotes or unquoted keys are invalid.
- JSON does not support comments. Use JSONC (JSON with Comments) if comments are needed.
- Use ISO 8601 format for date/time (e.g., "2026-07-19T10:30:00Z").
FAQ
How do I represent a newline inside a JSON string?
Literal newline characters are not allowed inside JSON strings; they must be escaped as \n (and tabs as \t). Using JSON.stringify(value) does this automatically. Chinese characters can stay as-is in UTF-8 and usually need no escaping.
What causes most JSON errors and how do I locate them fast?
The three usual suspects are missing double quotes on keys, a trailing comma at the end of an object or array, and unbalanced braces or quotes. Validate with jq . to get the line and error type; in the browser wrap JSON.parse in try/catch and log the failing fragment, then narrow it by bisecting.
Do I need to escape Chinese characters in JSON?
Not required by the spec — Chinese written directly as UTF-8 is valid and parsed fine by most tools. Only if a channel insists on ASCII (legacy headers or log pipelines) should you convert to \uXXXX escapes, e.g. via the -ascii option or equivalent tooling.
Is writing JSON numbers as quoted strings a problem?
It parses fine but changes semantics: "123" is a string while 123 is a number, affecting typeof, math and lexicographic sorting. Use strings for very long IDs or values with leading zeros; otherwise write numbers literally, and use true/false rather than the strings true or false.
How do I quickly validate nested or large JSON and extract values?
jq is the handiest: jq . file.json validates and pretty-prints, jq '.key' reads a value, jq 'length' returns an array length, and jq empty file.json only checks validity. In the browser wrap JSON.parse in try/catch; for a visual sandbox use jqplay or an online formatter and validator.
Official References
Each command links to its official documentation below, so you can verify the latest usage and read deeper.
Maintained by LaoHand
Publicly updated on Jul 21, 2026, continuously proofread against official docs.
Contact Us
Wrong command or description? Send us corrections, business inquiries or product feedback by email.
Contact Us