The Most Common JSON Syntax Errors (and How to Spot Them Instantly)
JSON — JavaScript Object Notation — has an intentionally minimal syntax: six structural characters, two value types, and three literal names. Despite this simplicity, malformed JSON is one of the most frequent causes of API integration failures, configuration file errors, and data pipeline breakages. The parser is unforgiving: a single trailing comma, an unquoted key, or a stray comment will throw a parse error for the entire document.
Trailing Commas: The Biggest Culprit
JavaScript allows trailing commas in object and array literals. JSON does not. The following is valid JavaScript but invalid JSON:
{"name": "Alice", "age": 30,}
The trailing comma after 30 will cause JSON.parse() to throw a SyntaxError. When copying configuration snippets from documentation, always verify that trailing commas have been removed.
The Other Fatal Errors
- Unquoted keys:
{name: "Alice"}is JavaScript, not JSON. All keys must be double-quoted strings. - Single-quoted strings:
{'name': 'Alice'}is invalid. JSON requires double quotes exclusively. - Comments: JSON has no comment syntax. Neither
// commentnor/* comment */is valid inside a JSON document. Use JSONC (JSON with Comments) format only in contexts that explicitly support it, such as VS Code settings files. - Undefined and NaN: These JavaScript primitives do not exist in JSON. Serialise them as
nullor omit the key entirely.
Validating and Prettifying JSON
The fastest way to validate and format any JSON string client-side is to run it through JSON.parse() and immediately serialize it back with JSON.stringify(parsed, null, 2). The null, 2 arguments instruct the serialiser to indent with two spaces, producing human-readable output. Our JSON Formatter & Beautifier does exactly this in your browser — no data ever leaves your device.
JSON Schema Validation for APIs
For validating that a JSON document conforms to an expected structure — correct field names, correct data types, required fields present — JSON Schema is the standard. Tools like ajv (Node.js) or jsonschema (Python) validate a document against a schema definition at runtime, enabling robust API contract enforcement without writing manual validation logic.