How to Fix Common JSON Syntax Errors
JSON (JavaScript Object Notation) is governed by the strict RFC 8259 standard. Even a single stray character or quotation mismatch will cause parser crashes across V8, Python, Java, and Go runtime environments. Here is how to diagnose, fix, and prevent the most common syntax errors.
Understanding JSON Specification (RFC 8259)
Unlike standard JavaScript object literals, Python dictionaries, or loose config formats like JSON5, pure JSON does not allow syntax flexibility. Standard JSON strictly defines six data types: object, array, string, number, boolean (true/false), and null.
JSON parsers are deterministic state machines. A syntax failure at byte offset 1,420 aborts the entire parsing pipeline immediately, throwing fatal runtime exceptions.
1. Trailing Commas (The Most Frequent Bug)
Modern ECMAScript (ES6+) and Python allow trailing commas after the final element of an array or object property. In JSON, trailing commas are completely prohibited.
When a parser encounters a trailing comma followed by a closing bracket or brace, it expects another value token. Finding a closing delimiter instead triggers an immediate Unexpected token exception.
// ❌ Invalid: Trailing commas in array & object
{
"service": "payment-gateway",
"ports": [8080, 8443, ],
"timeout": 3000,
}
// ✅ Clean valid JSON
{
"service": "payment-gateway",
"ports": [8080, 8443],
"timeout": 3000
}
2. Single Quotes vs Double Quotes
In languages like JavaScript and Python, single quotes ('text') and double quotes ("text") are interchangeable. In JSON, all strings and property keys must be enclosed in double quotes.
// ❌ Invalid: Single quotes used for keys and string values
{
'username': 'dev_admin',
'active': 'true'
}
In Node.js / V8, single quotes throw: SyntaxError: Expected double-quoted property name in JSON at position 2. In Python: json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes.
3. Unquoted Object Keys
JavaScript object initializers allow identifiers without quotes (e.g. { count: 42 }). JSON parsers mandate explicit double quotation around every key identifier.
// ❌ Invalid: Unquoted keys
{ host: "127.0.0.1", port: 5432 }
// ✅ Correct: Double-quoted keys
{ "host": "127.0.0.1", "port": 5432 }
4. Comments in Production Payloads
Douglas Crockford intentionally excluded comments (// and /* */) from the JSON specification to prevent parsing ambiguities and parsing security loopholes. If your configuration file contains comments, strip them before serialization or use JSONC only in environments with custom comment-aware loaders.
5. Python Literal Leaks (, , )
When Python developers use str(dict_data) instead of json.dumps(dict_data), Python's capitalized keywords leak into output strings:
True→ must betrueFalse→ must befalseNone→ must benull
6. 64-Bit Integer Precision Loss (BigInt Pitfall)
JSON specification does not put a hard limit on number length, but IEEE 754 double-precision floating point standard in JavaScript caps safe integers at Number.MAX_SAFE_INTEGER (253 - 1 = 9,007,199,254,740,991).
Sending a 64-bit integer ID like 9007199254740993 as a raw JSON number causes JavaScript clients to silently round it to 9007199254740992. Always serialize 64-bit IDs and database snow-flake keys as quoted strings: "9007199254740993".
Quick Reference: Error Diagnostic Matrix
| Parser Error Message | Root Cause | Quick Remedy |
|---|---|---|
Unexpected token , in JSON |
Trailing comma after last property | Delete comma preceding } or ] |
Expected double-quoted property name |
Single quotes or missing quotes on key | Wrap key in "..." |
Unexpected token / in JSON |
Stray comment present | Remove // and /* */ comments |
JSONDecodeError: Expecting value |
Empty string, unescaped newline, or None | Replace None with null, escape newlines with
|
Fixing Errors Automatically in Seconds
Instead of manually searching through thousands of lines of malformed payloads, use the client-side repair engine in JSONLints Studio:
- Paste your broken payload into the editor.
- Click the Auto Repair button on the toolbar.
- JSONLints identifies line-and-column mismatches, quotes unquoted keys, replaces single quotes, strips comments, normalizes Python literals, and formats output instantaneously in your browser.