JSON Syntax Rules Explained, With Valid and Invalid Examples
JSON's grammar is small enough to learn completely. This guide walks through every rule in RFC 8259, with an example of what is valid and what is not, so you can read an error message and know exactly what to fix.
Objects
An object is zero or more name-value pairs inside curly braces. Each name is a string, followed by a colon, followed by a value. Pairs are separated by commas.
{ "name": "Ada", "age": 36 } valid
{ name: "Ada" } invalid: unquoted key
{ "name": "Ada", } invalid: trailing comma
{} valid: empty objectRFC 8259 says names should be unique. Duplicate keys are not a syntax error, but parsers behave differently: JavaScript keeps the last value, while some libraries reject the document. Avoid them.
Arrays
An array is an ordered list of values in square brackets, separated by commas. Values in one array do not have to be the same type, although APIs usually keep them consistent.
[1, 2, 3] valid
["a", 1, true, null] valid: mixed types
[1, 2, ] invalid: trailing comma
[] valid: empty arrayStrings and escape sequences
Strings are wrapped in double quotes. Inside a string, a double quote, a backslash and control characters (such as a real line break) must be escaped with a backslash.
| Escape | Meaning |
|---|---|
| \" | Double quote |
| \\ | Backslash |
| \/ | Forward slash (optional) |
| \n | Line feed |
| \r | Carriage return |
| \t | Tab |
| \b | Backspace |
| \f | Form feed |
| \u00e9 | Any character by its UTF-16 code unit, here é |
A real line break inside a string is invalid; write \n instead. Characters outside the Basic Multilingual Plane, such as many emoji, can be written directly or as a surrogate pair like \ud83d\ude00. Use the JSON Escape tool to escape text correctly.
Numbers
Numbers are written in decimal, with an optional minus sign, fraction and exponent.
42 -7 3.14 0.5 1e10 2.5E-3 valid
+1 invalid: no leading plus
.5 5. invalid: digits needed on both sides of the point
007 invalid: no leading zeros
0x1F invalid: no hexadecimal
NaN Infinity invalidJSON itself sets no size limit, but most parsers store numbers as 64-bit floating point. Integers larger than 253 (9,007,199,254,740,992) can silently lose precision in JavaScript, which is why many APIs send large IDs as strings.
true, false and null
The three literal names must be lowercase: true, false, null. True, NULL and None are invalid. There is no undefined.
Whitespace
Spaces, tabs, line feeds and carriage returns are allowed between tokens and are ignored. That is why formatting and minifying JSON never changes its meaning: the JSON Formatter adds whitespace and JSON Minify removes it.
Encoding
JSON exchanged between systems must be encoded as UTF-8 (RFC 8259). A byte-order mark (BOM) at the start is not allowed by the standard, although some parsers tolerate it; if a file fails at line 1, column 1, an invisible BOM is a likely cause.
Common syntax mistakes at a glance
- Single quotes instead of double quotes.
- Unquoted keys.
- A trailing comma after the last item.
- Comments.
- Python or JavaScript literals:
True,None,undefined. - A literal line break inside a string.
- A missing comma between two items, often after copying lines around.
Frequently asked questions
Does JSON allow comments?
No. Comments were deliberately left out of the format. Use JSON5 or YAML for configuration that needs comments.
Are keys case-sensitive?
Yes. "Name" and "name" are different keys.
Is the order of keys significant?
The standard treats objects as unordered, so code should not rely on key order. Most parsers do keep the order in which keys appear.
Must I escape the forward slash?
No. \/ is allowed but optional; it is sometimes used so that "</script>" cannot appear inside JSON embedded in HTML.