The Syntax Error in This Example
The input above is almost valid JSON. The problem is the trailing comma after false in the second user object:
{"name": "Bob", "active": false,}
Remove that comma and the document is valid:
{
"users": [
{"name": "Alice", "active": true},
{"name": "Bob", "active": false}
]
}
This is the most common JSON error developers encounter, and it is easy to miss because JavaScript tolerates it.
What Makes JSON Valid
JSON syntax is defined by RFC 8259. The rules are simpler than most people expect. A valid JSON document is one of: an object, array, string, number, boolean (true or false), or null. Objects are collections of key-value pairs. Keys must be strings (double-quoted). Values can be any JSON value. Arrays are ordered lists of JSON values.
The structural rules that trip developers up most often:
Trailing commas
The last element in an array or the last key in an object must not have a trailing comma. [1, 2, 3,] is invalid. {"a": 1, "b": 2,} is invalid.
String quoting
All strings must use double quotes. 'Alice' is not valid JSON. "Alice" is.
Key quoting
Object keys must be quoted strings. {name: "Alice"} is not JSON. {"name": "Alice"} is.
No comments
JSON has no comment syntax. // comment and /* comment */ will cause a parse failure. This surprises people who come from languages where config files support comments.
Special float values
NaN, Infinity, and -Infinity are not valid JSON values. If you try to serialize a JavaScript NaN to JSON with JSON.stringify(), you get null. Python’s json.dumps() raises a ValueError by default.
The 5 Most Common JSON Syntax Mistakes
| Mistake | Example | Fix |
|---|---|---|
| Trailing comma | {"a": 1,} | {"a": 1} |
| Single-quoted strings | {'key': 'value'} | {"key": "value"} |
| Unquoted keys | {key: "value"} | {"key": "value"} |
| Comments | {"a": 1 // note} | Remove comments |
| NaN or undefined | {"val": NaN} | Replace with null |
Validating JSON on the Command Line
For quick validation without a browser, jq is the standard tool. It exits with a non-zero code if the input is invalid JSON, which makes it useful in scripts.
Validate a file:
jq . file.json
Validate a string:
echo '{"name": "Alice",}' | jq .
# parse error (Expected another key-value pair or '}')
Python’s standard library also validates without any dependencies:
python3 -m json.tool file.json
For CI pipelines, both tools work as validation steps that fail loudly on malformed input.
JSON vs JSONC vs JSON5
| Format | Comments | Trailing commas | Single quotes | Use case |
|---|---|---|---|---|
| JSON | No | No | No | Data interchange, APIs |
| JSONC | Yes | Yes | No | Config files (VS Code) |
| JSON5 | Yes | Yes | Yes | Config files (Babel, ESLint) |
If you are building an API or writing data that will be parsed by a library, use strict JSON. If you are writing a config file that a specific tool reads, check what format that tool expects. Mixing them up is a common source of silent failures when config files get passed to standard parsers.
The Spec
RFC 8259 is the current JSON specification, published in 2017. It replaced RFC 7159. The spec is readable in about 20 minutes and worth skimming once. It defines the grammar formally, covers number precision, and specifies UTF-8 encoding. One key point from the spec: a JSON parser is permitted to accept or reject duplicate keys in an object. Most parsers accept them and use the last value, but behavior is undefined and you should not rely on it.