JSON Cheat Sheet
JSON syntax, escapes, JavaScript methods and error fixes.
Value types
"text"String: always double quotes
42, -3.14, 1e3Number: no leading zeros, no NaN or Infinity
true / falseBoolean (lowercase)
nullNull (lowercase)
[1, "a", null]Array: ordered list of any values
{ "key": "value" }Object: string keys with values
Syntax rules
Double quotes only'single quotes' are invalid JSON
No trailing commas[1, 2,] is a syntax error
No comments// and /* */ are not allowed
Keys must be strings{ a: 1 } is JavaScript, not JSON
One root valueAny value is allowed at the root, not only objects
UTF-8 textThe standard encoding for JSON on the wire
String escapes
\"Double quote
\\Backslash
\/Forward slash (optional)
\n \r \tNewline, carriage return, tab
\b \fBackspace, form feed
\u00e9Unicode code unit (use surrogate pairs above U+FFFF)
JavaScript
JSON.parse(text)Text to value; throws on invalid JSON
JSON.stringify(value)Value to compact JSON
JSON.stringify(value, null, 2)Pretty print with 2 spaces
JSON.stringify(value, ["a", "b"])Only include the listed keys
JSON.parse(text, (key, val) => val)Reviver: transform values while parsing
toJSON() { return ... }Custom serialization method on an object
Command line (jq)
jq .Pretty print
jq .nameRead a field
jq ".items[0].id"Index into an array
jq ".items[] | .id"Iterate over an array
jq -c .Compact output
jq "map(select(.age > 30))"Filter an array
Common errors
Unexpected token < at position 0You received HTML (often an error page) instead of JSON
Unexpected end of JSON inputEmpty or truncated text
Unexpected token } or ]Trailing comma or a missing value
Bad control character in stringA raw newline or tab inside a string; escape it
JSON Cheat Sheet
JSON (JavaScript Object Notation) is the most common format for APIs, configuration files and data exchange. It is small and easy to read, but it is also strict, and one stray comma can break a whole document. This cheat sheet gathers the rules and the everyday commands in one place, with a search box and copy buttons.
What you will find
- Value types – strings, numbers, booleans, null, arrays and objects, with the exact syntax for each.
- Syntax rules – double quotes only, no trailing commas, no comments, string keys.
- String escapes – how to write quotes, backslashes, newlines, tabs and Unicode characters inside strings.
- JavaScript methods – JSON.parse, JSON.stringify, pretty printing, key filtering, revivers and toJSON.
- Command line – a few jq filters for pretty printing, reading fields and filtering arrays.
- Related formats – MIME type, NDJSON, JSON5 and JSON Schema.
- Common errors – what messages such as "Unexpected end of JSON input" really mean and how to fix them.
Why JSON breaks
Most parse errors fall into a few groups: a trailing comma after the last item, single quotes instead of double quotes, an unquoted key, a comment, a raw newline inside a string, or a response that is not JSON at all. Paste the document into a validator or formatter to see the exact line and column. Pretty printing with two spaces also makes missing brackets easy to spot.
Good habits
Keep numbers within the range of a double precision float (very large IDs are safer as strings). Use ISO 8601 for dates, such as 2026-01-31T12:00:00Z. Choose one naming style for keys, usually camelCase or snake_case. Version your API payloads and validate incoming data with JSON Schema before you trust it.
Frequently Asked Questions
Can JSON have comments?
No. Standard JSON does not allow comments. If you need them in configuration files, use JSONC or JSON5 and strip the comments before sending the data to a strict parser.
Why do I get "Unexpected token < in JSON at position 0"?
The text you tried to parse starts with a less-than sign, which almost always means the server returned an HTML page (an error or login page) instead of JSON. Check the response status and the URL.
Are single quotes allowed in JSON?
No. Strings and keys must use double quotes. Single-quoted text is valid JavaScript but invalid JSON.
What is the difference between JSON and a JavaScript object?
JSON is a text format with strict rules: quoted keys, double-quoted strings and no functions, undefined or comments. A JavaScript object is an in-memory value that can hold many more things. Use JSON.stringify and JSON.parse to convert between them.