JSON Cheat Sheet

JSON syntax, escapes, JavaScript methods and error fixes.

​
Value typesSyntax rulesString escapesJavaScriptCommand line (jq)Related formats and MIME typesCommon errors
39 entries

Value types

"text"

String: always double quotes

42, -3.14, 1e3

Number: no leading zeros, no NaN or Infinity

true / false

Boolean (lowercase)

null

Null (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 value

Any value is allowed at the root, not only objects

UTF-8 text

The standard encoding for JSON on the wire

String escapes

\"

Double quote

\\

Backslash

\/

Forward slash (optional)

\n \r \t

Newline, carriage return, tab

\b \f

Backspace, form feed

\u00e9

Unicode 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 .name

Read 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 0

You received HTML (often an error page) instead of JSON

Unexpected end of JSON input

Empty or truncated text

Unexpected token } or ]

Trailing comma or a missing value

Bad control character in string

A 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

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.

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.

No. Strings and keys must use double quotes. Single-quoted text is valid JavaScript but invalid JSON.

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.