Zum Hauptinhalt springen

JSON Syntax Cheat Sheet

JSON syntax, as defined by RFC 8259, reduces to six structural characters, four legal whitespace characters, two containers, six value types and a strict grammar for strings and numbers. This cheat sheet puts every rule on one page — including the exact mistakes parsers reject, from trailing commas to hexadecimal literals.

Nur lokal

Die folgende Anleitung ist nur auf Englisch verfügbar.

JSON Syntax Cheat Sheet explained

The full JSON grammar fits in a few dozen lines of ABNF, and almost every real-world parsing error is one of a dozen specific mistakes. This page is the reference version of that knowledge: each section states one slice of the grammar exactly as RFC 8259 defines it, then shows the rule as copyable JSON you can paste into a formatter or validator and watch pass or fail.

Three documents matter if you want primary sources. RFC 8259 is the internet standard in force, ECMA-404 is the identical twin standard published by Ecma International, and the number grammar inside RFC 8259 deliberately reuses the ECMAScript specification's number grammar with its octal and hexadecimal extensions removed. Every rule below traces to one of those three sources — nothing here is convention or folklore.

The rules are short but unforgiving: there is no lenient JSON. A conforming parser accepts a document whole or rejects it with a syntax error, and the rejections cluster around the handful of rules developers forget most often — quoting style, trailing commas, comments, leading zeros. Learn those once below and most validation failures stop being mysteries.

Rules stick fastest when you break them on purpose, so keep the JSON Formatter open in a second tab and re-indent every example on this page as you read it.

When you want a ruling rather than a pretty print, the JSON Validator reports the exact line and column of the first rule a document breaks.

The six structural characters

Six punctuation characters carry all of the structure in JSON: the braces { } that wrap objects, the brackets [ ] that wrap arrays, the colon : that separates a key from its value, and the comma , that separates pairs inside an object or values inside an array. Nothing else in the grammar does structural work — every other character in a document is either inside a string or is one of the four whitespace characters. A comma is strictly a separator: it may appear between two items, never before a closing brace or bracket.

CharactersNameJob in the grammar
{ }BracesOpen and close an object — a set of key/value pairs
[ ]BracketsOpen and close an array — an ordered list of values
:ColonSeparates a key from its value inside an object
,CommaSeparates pairs or array items — never the last thing before a closer

Valid — commas only between items

{"name":"Ada","tags":["math","pioneer"]}

Invalid — trailing comma before a closer

{"name":"Ada","tags":["math","pioneer"],}

Whitespace: exactly four characters

Outside strings, JSON permits exactly four whitespace characters: the space (U+0020), the horizontal tab (U+0009), the line feed (U+000A) and the carriage return (U+000D). Anything else — vertical tab, form feed, non-breaking space, a byte order mark — is a syntax error, even though many editors render those characters invisibly. That is why JSON pasted from a word processor so often fails validation: the document looks clean on screen and contains characters the grammar has never heard of.

Whitespace is also what makes formatting possible. Because the grammar ignores these four characters between tokens, the same document can be one long line on the wire and an indented tree in your editor, and the two forms are semantically identical — no parser can tell them apart after reading.

Minified — no optional whitespace

{"a":1,"b":[2,3]}

Pretty-printed — whitespace added, data unchanged

{
  "a": 1,
  "b": [
    2,
    3
  ]
}

Strings: double quotes and the nine escapes

A JSON string is wrapped in double quotes — never single quotes. Inside, the backslash \ introduces an escape, and RFC 8259 defines exactly nine two-character escapes: two are mandatory, \\" for a literal double quote and \\\\ for a literal backslash, because those two characters would otherwise end or corrupt the string. Six more are optional shorthands — \/ for the forward slash and \b \f \n \r \t for the five control characters that appear most often in text. The ninth form is \uXXXX, which writes any Unicode code point by its four-digit hexadecimal number.

Every character outside the escape set may appear literally as UTF-8, and the two forms are equivalent: a parser delivers the identical string either way. Escaping is spelling, not data — a point the escaping guide develops in depth.

EscapeProducesStatus per RFC 8259
\"double quoteMandatory — the only way to quote inside a string
\\backslashMandatory — the only way to write a literal backslash
\/forward slashOptional shorthand, legal but rarely needed
\b \f \n \r \tbackspace, form feed, newline, return, tabNamed escapes for the five common control characters
\uXXXXany code point, e.g. \u00e9 for éUniversal form — exactly four hex digits

Escaped form in the file

"quote": "she said \"hi\""
"path": "C:\\Users\\ada"
"e-acute": "\u00e9"

What a parser hands back

quote:   she said "hi"
path:    C:\Users\ada
e-acute: é

Numbers: ECMAScript, minus the extensions

A number is an integer or decimal in plain notation, with an optional exponent. RFC 8259 adopts the ECMAScript number grammar and removes its extensions: no hexadecimal, no octal, no leading zeros, no leading plus, no bare trailing dot. An exponent uses e or E with an optional sign, and the only sign a number may start with is the plain hyphen-minus. NaN, Infinity and -Infinity are not JSON — languages that have such constants write null in their place by convention.

Precision is deliberately left to implementations: the grammar says nothing about how many digits a parser must honour, so very long numbers can arrive silently changed. Interoperable producers keep integers inside the range the target platform represents exactly — for the IEEE 754 doubles behind JavaScript, that is up to 2^53 − 1.

WrittenVerdictWhy
42, -17, 3.14ValidPlain integer and decimal forms
2.1e10, 5E-3ValidExponents: e or E, optional sign after it
01, -01InvalidLeading zeros are forbidden — write 1
1., .5, +6InvalidA dot needs digits on both sides; no leading plus
0x1F, 1_000InvalidHexadecimal and digit separators are JavaScript extensions
NaN, InfinityInvalidNot part of the grammar; JSON has no such constants
9007199254740993RiskyParses, but exceeds the exact-integer range of IEEE 754 doubles

The three literals and the five classic rejections

Three bare tokens complete the value set: true, false and null. They are lowercase and unquoted — True, FALSE and None are syntax errors, not creative capitalisation. Around them, the grammar draws five lines that account for nearly every rejected document in the wild:

  • No comments. RFC 8259 has no comment syntax; // or /* */ anywhere is a syntax error. Annotate with a sibling key like "_comment" where the format is under your control.
  • No trailing commas. [1, 2, 3,] and {"a":1,} are invalid, however friendly they look — the comma is a separator, and it cannot separate an item from nothing.
  • No single quotes and no unquoted keys. {name:'ada'} is invalid JSON; {"name":"ada"} is the grammar's only spelling.
  • No duplicate keys — officially. The grammar permits a name to appear twice, but RFC 8259 §4 leaves which value wins to implementations, so a document that relies on duplicates has no defined meaning.
  • Any top-level value. Since RFC 8259, a whole document may be a bare string, number, boolean or null — RFC 4627's objects-and-arrays-only rule is gone, and "hello" or 42 are complete JSON documents.

Encoding: UTF-8 and the byte-level rules

RFC 8259 §8.1 fixes the byte encoding: JSON text exchanged between systems that are not part of a closed ecosystem must be encoded in UTF-8. There is no UTF-16 or UTF-32 JSON on the open wire, and producers must not prepend a byte order mark — a parser may ignore a leading BOM rather than error, but nothing conforming emits one. Note how the layers separate: \uXXXX escaping is a notation choice inside individual strings, while UTF-8 is a property of the whole document, and the two solve different problems.

That closes the grammar. The fastest way to make these rules permanent is to run your own documents against them — format a real payload, break one rule at a time, and read each validator error. Ten minutes of deliberate failure teaches more than another pass over this page.

Frequently asked questions

Can I paste real API data into the tools linked from this page?

Yes. The formatter and validator on this site run entirely in your browser: your document is parsed by JavaScript inside the tab, and there is no upload step to audit. You can confirm it yourself — the browser's Network panel stays silent while the tools work, and the site's Content-Security-Policy blocks outbound connections, so even a modified copy of the page could not transmit your data.

Does JSON allow single quotes for strings?

No. RFC 8259 defines strings as double-quoted, for keys and values alike, and gives single quotes no meaning at all — a parser meeting 'hello' fails immediately, while "hello" is the only valid spelling. Tools that happily read single quotes are parsing JSON5 or another superset, not JSON, which is why their output can still be rejected by stricter consumers.

What exactly is a trailing comma and why is it an error?

Position, not character. A comma between two items is a separator; a comma immediately before } or ] would separate an item from nothing, and the grammar forbids that. It is the single most common reason a hand-edited file fails validation, which is why generators, minifiers and formatting tools all strip trailing commas for you before writing.

Why can't JSON numbers have leading zeros like 007?

Leading zeros were how octal (base-8) literals were written in older JavaScript, and RFC 8259 takes its number grammar from ECMAScript with the octal and hexadecimal extensions removed — so 007 is rejected to keep one spelling meaning one value. Write 7, and store identifiers such as postal codes as strings whenever a leading zero is part of the value.

Are duplicate keys in a JSON object legal?

The grammar allows a name to appear twice, but the standard deliberately refuses to define the result: RFC 8259 notes that implementations report the last value, the first value, or an error. A document whose meaning depends on duplicates is therefore not interoperable JSON — treat duplicates as a bug even when your particular parser happens to accept them silently.

How large can a JSON number be before precision breaks?

The grammar sets no digit limit, but most parsers decode into IEEE 754 double-precision floats, which represent integers exactly only up to 2^53 − 1. A 17-digit identifier such as 9007199254740993 can arrive silently altered — the classic reason SDKs for APIs with 64-bit ids instruct you to treat them as strings instead of numbers.