JSON looks deceptively simple, but its grammar is strict and unforgiving. A single misplaced comma can cause a parser to throw Unexpected token and bring an entire API integration to a halt. This guide covers the errors developers hit most often and shows the exact fix for each. You can paste any snippet into our JSON Formatter to find the offending line instantly.
1. Trailing commas
JavaScript object literals allow a comma after the last element. JSON does not. This is the single most common JSON error.
// Invalid โ trailing comma after 30
{ "name": "John", "age": 30, }
// Valid
{ "name": "John", "age": 30 }2. Single quotes instead of double quotes
JSON requires double quotes for both keys and string values. Single quotes are valid in JavaScript but illegal in JSON.
// Invalid
{ 'name': 'John' }
// Valid
{ "name": "John" }3. Unquoted keys
Every key must be a double-quoted string. Bare identifiers, which JavaScript permits, are rejected.
// Invalid
{ name: "John" }
// Valid
{ "name": "John" }4. Comments
Standard JSON does not support // or /* */ comments. If you need comments in config files, use a superset like JSON5 or YAML, then convert to JSON for machines.
5. Wrong value types
JSON values must be a string, number, boolean, null, object, or array. These common JavaScript values are not valid JSON:
undefinedโ usenullinsteadNaNandInfinityโ not representable- Function values or dates as objects โ serialize to strings first
- Numbers with leading zeros or a trailing dot (
.5,05)
6. Unescaped special characters in strings
Literal newlines, tabs, and double quotes inside a string must be escaped with a backslash.
// Invalid โ raw quote and newline
{ "note": "She said "hi"
and left" }
// Valid
{ "note": "She said \"hi\"\nand left" }How to debug JSON quickly
- Run it through a formatter โ it will point to the exact failing line.
- Watch for mismatched brackets and braces; an editor that highlights pairs helps.
- If an API returns broken JSON, log the raw response before parsing.
- Validate against a schema when the structure matters, not just the syntax.
Frequently asked questions
Why does my JSON fail only in production?
Often the data differs: a field that is always present in test data may be undefined or contain unescaped characters in real traffic. Log and validate the actual payload.
What is the difference between JSON and a JavaScript object?
JSON is a text format with strict rules. A JavaScript object is an in-memory data structure with looser syntax. All JSON is valid JavaScript, but not all JavaScript objects are valid JSON.