When building web applications and backend APIs, passing dynamic string data into JSON.parse() is a frequent source of runtime crashes. This guide details the exact RFC 8259 syntax specifications, why parsers throw unexpected token errors, and how to debug and repair malformed JSON payloads client-side.
JSON is an interchangeable, text-based data format designed to be completely language-agnostic across C, Python, Go, Rust, Java, and JavaScript. To prevent ambiguous parsing across different runtime environments, the parser operates as a strict deterministic state machine. If an unexpected token (such as a single quote or an unescaped control character) is encountered, JSON.parse() aborts with a SyntaxError.
In modern ECMAScript, trailing commas in objects and arrays are permitted for clean git diffs. However, in RFC 8259 JSON, a comma explicitly signals that another element follows. A trailing comma leaves the parser expecting another value before the closing brace or bracket.
Remove the final comma preceding the closing curly brace.
{
"userId": 104,
"role": "admin",
}{
"userId": 104,
"role": "admin"
}Paste malformed JSON into JSON Validator to highlight the exact row and column of the dangling comma.
{
"name": "Alex",
"active": true,
}RFC 8259 Section 7 requires string literals and property names to be wrapped exclusively in double quotation marks ("). Single quotes (') and unquoted JavaScript identifiers are invalid.
{
name: 'Alice',
status: 'active'
}{
"name": "Alice",
"status": "active"
}The backslash (\) is reserved as an escape prefix. Writing Windows file paths like "C:\Users\name" fails because \U is not a recognized escape sequence. Backslashes must be double-escaped as "\\".
{
"path": "C:\Users\admin\documents"
}{
"path": "C:\\Users\\admin\\documents"
}The table below details the exact syntax differences between in-memory JavaScript object literals and serialized JSON text payloads:
| Feature | JSON (RFC 8259) | JavaScript Object Literal |
|---|---|---|
| Key Quotes | Double quotes mandatory ("key") | Optional for valid identifiers (key) |
| String Quotes | Double quotes only ("text") | Single ('), double ("), or backticks (`) |
| Trailing Commas | Strictly Forbidden | Allowed (ES2017+) |
| Comments | Forbidden (// or /* */) | Supported (// and /* */) |
| Functions / Methods | Not supported (omitted) | Supported (greet() {}) |
| undefined | Not supported (omitted/throws) | Supported |
| Date Objects | Serialized to ISO 8601 String | Supported as Date instances |
Log or extract the exact text string before JSON.parse() execution. Verify the response is not HTML (e.g. 404/500 error page starting with <!DOCTYPE html>).
Paste the string into JSON Validator. The validator parses the AST and marks the exact row, column, and character causing the syntax breakdown.
If JSON appears visually correct but still crashes, scan for zero-width spaces (U+200B) or byte order marks (U+FEFF).
Format the corrected payload into clean 2-space indentation with JSON Formatter.
• Copying console.log output directly into a JSON file without serializing with JSON.stringify(). • Assuming JSON5 or JSONC settings in VS Code (such as tsconfig.json) apply to HTTP REST APIs. • Pasting multi-line template literal strings containing raw unescaped newlines. • Attempting to serialize BigInt values without a custom serializer function (throws TypeError: Do not know how to serialize a BigInt).
Format, pretty-print, validate, and minify JSON data with diagnostic error tracking.
Check JSON syntax validity, pinpoint matching brackets, and diagnose syntax errors.
Inspect Unicode code points, UTF-8 bytes, UTF-16 units, and detect zero-width characters.