Debugging Common JSON Syntax Errors: How to Fix Trailing Commas, Unescaped Quotes, and Encoding Mismatches
JavaScript Object Notation (JSON) is the universal data interchange format for modern APIs, yet strict RFC 8259 parsing rules frequently cause baffling exceptions. Here is how to diagnose and resolve them.
Why JSON Parsers Are Unforgiving (RFC 8259)
Unlike JavaScript object literals—which permit trailing commas, single quotes, unquoted keys, and inline comments—standard JSON (RFC 8259 / ECMA-404) is deliberately strict. Parsers in languages such as Python (json.loads()), Go (json.Unmarshal()), and native browser engines (JSON.parse()) will instantly throw fatal exceptions upon encountering any non-compliant token.
The Top 4 Common JSON Syntax Pitfalls
1. Trailing Commas in Objects and Arrays
In modern ECMAScript, trailing commas in objects and arrays are valid. In JSON, they are strictly forbidden:
2. Single Quotes vs. Double Quotes
JSON specification section 7 mandates that all string literals and object keys must be enclosed in double quotes ("). Single quotes (') are never recognized as string delimiters.
3. Unescaped Control Characters & Quotes
Strings containing internal double quotes or newline characters must be explicitly escaped with reverse solidus characters:
4. Leading Zeroes on Numeric Literals
In standard JSON, numbers cannot have leading zeroes (e.g., 0145 is invalid and causes a syntax error). They must be represented either as integers (145) or as string identifiers ("0145").
Client-Side Safe Parsing in JavaScript
When building client-side tools, robust error handling extracts the exact line and column of the syntax error to guide the user: