How to Fix Broken JSON: Common Errors and How to Repair Them
JSON parse errors are among the most common errors in web development. The error messages are often cryptic — Unexpected token ',', Expected property name or '}', Unterminated string — and they point to a line number that isn't always where the actual problem is. This guide explains every common JSON error, shows what causes it, and gives you the exact fix.
If you just need the JSON fixed without debugging it manually, paste it into the JSON Repair tool — it automatically detects and fixes the most common issues. The rest of this article explains what each error means so you understand why it broke.
Error 1: Trailing Comma
The most common JSON error. JSON does not allow a comma after the last item in an array or object.
// ❌ Invalid JSON — trailing comma in array
["apple", "banana", "cherry",]
// ❌ Invalid JSON — trailing comma in object
{
"name": "Alice",
"role": "admin",
}
// ✅ Valid JSON
["apple", "banana", "cherry"]
{
"name": "Alice",
"role": "admin"
}Why it happens: Most programming languages (JavaScript, Python, Go after Go 1.23, Rust) allow trailing commas in array and object literals. Developers copy code directly into JSON or write config files with the same habits. The JSON spec explicitly forbids trailing commas.
Fix: Remove the comma after the last item in each array and object. If you have many, use the JSON Repair tool to fix all of them at once.
Error 2: Single-Quoted Strings
JSON requires double quotes. Single-quoted strings are JavaScript syntax, not JSON.
// ❌ Invalid JSON — single quotes
{
'name': 'Alice',
'role': 'admin'
}
// ✅ Valid JSON — double quotes
{
"name": "Alice",
"role": "admin"
}Why it happens: JSON looks like a JavaScript object literal, and in JavaScript single quotes and double quotes are interchangeable. In JSON they are not — the spec only allows double quotes for both keys and string values.
Fix: Replace all single quotes with double quotes. Be careful if your values contain apostrophes — it's should become "it's"... wait, no. In JSON you do not escape single quotes. "it's" is valid — only double quotes inside a string need escaping as ".
Error 3: Unquoted Keys
JSON object keys must always be in double quotes. Unquoted keys are valid in JavaScript but not in JSON.
// ❌ Invalid JSON — unquoted keys
{
name: "Alice",
role: "admin"
}
// ✅ Valid JSON — quoted keys
{
"name": "Alice",
"role": "admin"
}Why it happens: JavaScript object literals allow unquoted keys. When developers console.log an object and copy the output, or write config manually, the keys often come out unquoted.
Fix: Wrap every key in double quotes. In your editor, a regex find-and-replace can help: find (\w+): at the start of object entries and replace with "$1":. Or paste into the JSON Repair tool.
Error 4: Comments
JSON does not support comments. Not //, not /* */, not #.
// ❌ Invalid JSON — comments
{
// This is the user object
"name": "Alice",
"role": "admin" /* administrator */
}
// ✅ Valid JSON — no comments
{
"name": "Alice",
"role": "admin"
}Why it happens: Config files often need documentation. Developers who write YAML, JSONC, or INI files habitually add comments. JSON intentionally excludes comments — if you need annotated config, consider JSONC (supported by VS Code, TypeScript's tsconfig), JSON5, or YAML instead.
Fix: Strip all comments before parsing. The JSON Repair tool strips single-line and multi-line comments automatically.
Error 5: Mismatched or Missing Brackets
Every { needs a }, every [ needs a ], and they must be properly nested.
// ❌ Invalid JSON — missing closing bracket
{
"users": [
{"name": "Alice"},
{"name": "Bob"}
}
// ✅ Valid JSON
{
"users": [
{"name": "Alice"},
{"name": "Bob"}
]
}Why it happens: Usually from manual editing — deleting a line accidentally removes a bracket, or copy-pasting partial JSON leaves the structure incomplete.
Fix: Use the JSON Formatter to pretty-print the JSON first — mismatched brackets become visually obvious when indented. The validator will report the exact line where the parser gave up, which is typically just after the last valid structure. Work backwards from that line to find the missing bracket.
Error 6: Unescaped Special Characters in Strings
Inside a JSON string, certain characters must be escaped with a backslash.
// ❌ Invalid JSON — unescaped double quote and backslash
{
"message": "He said "hello" to me",
"path": "C:\Users\Alice"
}
// ✅ Valid JSON — properly escaped
{
"message": "He said \"hello\" to me",
"path": "C:\\Users\\Alice"
}Characters that must be escaped inside JSON strings:
| Character | Escaped form | Notes |
|---|---|---|
| " | \" | Double quote (terminates the string if unescaped) |
| \ | \\ | Backslash (Windows file paths) |
| / | \/ | Forward slash (optional — / is valid unescaped too) |
| newline | \n | Actual newlines inside strings must be escaped |
| tab | \t | Tab character |
| carriage return | \r | Windows line endings in strings |
Why it happens: When JSON is constructed by string concatenation rather than using a proper serializer, unescaped characters slip through. Always use JSON.stringify() in JavaScript, json.dumps() in Python, or json.Marshal() in Go to generate JSON — never build it by hand.
Quick Reference: JSON Parse Error Messages
| Error message | Likely cause |
|---|---|
| Unexpected token ',' | Trailing comma before } or ] |
| Unexpected token '}' | Trailing comma inside object, or missing value |
| Unexpected token '''' | Single-quoted string instead of double-quoted |
| Expected property name or '}' | Unquoted key, or trailing comma in object |
| Unexpected token '/' | Comment (// or /*) inside JSON |
| Unterminated string | Missing closing double quote, or actual newline inside string |
| Unexpected end of JSON input | Truncated JSON, or missing closing bracket/brace |
| Unexpected token 'u' | undefined value — JSON does not support undefined |
| Unexpected token 'N' | NaN value — JSON does not support NaN (use null instead) |
How to Auto-Repair Broken JSON
If you have large or complex broken JSON, fixing it manually is error-prone. The JSON Repair tool handles the most common errors automatically:
- Trailing commas in arrays and objects
- Single-quoted strings
- Unquoted keys
- JavaScript-style comments (
//and/* */) - Missing quotes around string values
- Truncated JSON (attempts structural completion)
Paste the broken JSON into the tool and click Repair. The output is valid, formatted JSON ready to use. Everything runs in your browser — nothing is sent to a server.
How to Prevent Broken JSON
The best fix is to never produce invalid JSON in the first place. Always use your language's built-in JSON serializer:
// JavaScript — always use JSON.stringify, never build JSON by hand
const json = JSON.stringify({ name: "Alice", role: "admin" });
// '{"name":"Alice","role":"admin"}'
// Python — always use json.dumps
import json
data = json.dumps({"name": "Alice", "role": "admin"})
# '{"name": "Alice", "role": "admin"}'
// Go — always use json.Marshal
import "encoding/json"
type User struct {
Name string `json:"name"`
Role string `json:"role"`
}
data, err := json.Marshal(User{Name: "Alice", Role: "admin"})
// {"name":"Alice","role":"admin"}These functions handle quoting, escaping, and structure correctly by construction. String concatenation to build JSON always eventually produces an error.
Validating JSON Before Using It
When receiving JSON from an external source — an API response, a file, a form input — validate it before parsing. Use the JSON Validator to check structure and get precise error locations (line and column) for any issues.
In code, wrap JSON.parse() in a try/catch:
// JavaScript — always handle parse errors
try {
const data = JSON.parse(input);
// use data
} catch (err) {
console.error("Invalid JSON:", err.message);
// err.message contains the position info: "Unexpected token at position 42"
}
// Python — always handle JSONDecodeError
import json
try:
data = json.loads(input)
except json.JSONDecodeError as e:
print(f"Invalid JSON at line {e.lineno}, column {e.colno}: {e.msg}")Have broken JSON to fix right now? Open the JSON Repair tool →
Frequently Asked Questions
What does 'Unexpected token' mean in a JSON parse error?▾
It means the JSON parser hit a character it didn't expect at that position. The most common causes: a trailing comma before } or ] (the parser expected a value, not a closing bracket), a single-quoted string (JSON requires double quotes), an unquoted key (JSON keys must be in double quotes), or a JavaScript comment (// or /* */ are not valid in JSON). The error message usually includes the character and position — use that to navigate to the exact line.
What is the difference between JSON and JavaScript object literal syntax?▾
JSON is a strict data interchange format derived from JavaScript but deliberately more restrictive. JavaScript object literals allow: unquoted keys, single-quoted strings, trailing commas, comments, undefined values, functions, and Date objects. JSON allows none of these. JSON only supports strings (double-quoted), numbers, booleans (true/false), null, arrays, and objects. This is why JSON.parse() throws on JavaScript object syntax even though it looks similar.
Can JSON have comments?▾
No. Comments are explicitly not part of the JSON specification. Douglas Crockford (the creator of JSON) intentionally excluded comments to prevent configuration files from being used to store parsing directives. If you need comments in a config file, consider JSONC (JSON with Comments, used by VS Code), JSON5, or YAML — all of which support comments. When consuming an API or parsing data programmatically, strip comments before passing to JSON.parse().
Why does my JSON look valid but still fail to parse?▾
Common invisible causes: (1) A BOM (Byte Order Mark) at the start of the file — some editors add a UTF-8 BOM that is invisible in most viewers. (2) Non-breaking spaces (U+00A0) that look like regular spaces but aren't. (3) Smart quotes (“”) inserted by word processors instead of straight double quotes. (4) Trailing whitespace after the closing bracket in a context that expects exactly one value. Paste the JSON into a validator — the error position will point directly to the invisible character.
How do I fix JSON that has trailing commas?▾
Remove the comma after the last item in each array or object. In an array: [1, 2, 3,] → [1, 2, 3]. In an object: {"a": 1, "b": 2,} → {"a": 1, "b": 2}. If you have many trailing commas (common in machine-generated or copy-pasted JSON), use the JSON Repair tool at devessentials.dev/json-repair — it detects and removes all trailing commas automatically.