How to Fix Invalid JSON: A Field Guide to Common Errors
Diagnose and repair invalid JSON—trailing commas, quotes, brackets, and encoding issues—with clear examples and a repair workflow.
Stay calm: invalid means unparsable
When a parser rejects JSON, something in the text violates the grammar. Your job is to locate the defect, fix it, and re-validate. Guessing wildly wastes time; a systematic approach does not.
Step 1: Get a precise error
Paste the document into a validator that reports line and column (or character offset). Read the message literally. “Expected ',' or '}'” usually means a missing comma or an extra trailing comma nearby—not a mysterious encoding curse.
Browser-local validators are ideal for customer data and tokens. JSON Editor is built for quick paste-validate-fix loops without uploading files.
Trailing commas
Invalid:
{
"a": 1,
"b": 2,
}
Valid: remove the comma after 2.
Trailing commas are legal in modern JavaScript—and illegal in JSON. That mismatch causes endless confusion.
Quotes and keys
Invalid:
{
name: "Sam",
'role': "admin"
}
Valid:
{
"name": "Sam",
"role": "admin"
}
Keys and strings use double quotes only.
Smart quotes from documents
Word processors convert "plain" into “curly”. JSON parsers reject curly quotes. Re-type quotes in a plain text editor or run a replace from Unicode quotation marks to ASCII " .
Brackets and braces
Count openings and closings. A missing ] after an array of objects is extremely common when editing by hand:
{
"items": [
{"id": 1},
{"id": 2}
}
The array never closes. Pretty-printing after a partial fix often reveals the imbalance visually.
Unescaped characters in strings
Inside strings you must escape:
"as\"\as\\- control characters via
\n,\t, etc.
Raw line breaks inside a string value are invalid. Use \n instead.
Comments and JavaScript leftovers
{
// user id
"id": 1
}
Comments are not standard JSON. Strip them or use a JSONC-aware toolchain intentionally—and do not ship JSONC to strict parsers expecting JSON.
Numbers and literals
NaN, Infinity, and undefined are not valid JSON. Use null, omit the key, or encode a string if you must preserve a sentinel—then document it.
Repair workflow summary
- Validate; note position.
- Inspect ±5 lines around the error.
- Fix the smallest thing (comma, quote, bracket).
- Validate again; repeat until green.
- Pretty-print and skim for structural sense.
- Only then apply schema or business checks.
Prevention beats repair
Generate JSON from serializers whenever possible. When hand-editing is required, edit pretty-printed files and validate before committing. Keep fixtures in CI so invalid JSON never merges quietly.
Invalid JSON is rarely mystical. It is almost always a comma, a quote, or a bracket—find it with a good error position, fix it once, and move on.
Related articles
Guides · 3 min read
JSON Parsing Guide: From Text to Typed Data Safely
How JSON parsing works across languages, common pitfalls with numbers and dates, and safer patterns for production code.
Guides · 3 min read
JSON Schema Guide: Contracts That Keep APIs Honest
An introduction to JSON Schema for validating structure, types, and required fields with practical examples and workflow tips.
Guides · 3 min read
JSONPath Explained: Query JSON Without Writing Loops
Learn JSONPath syntax, common selectors, practical examples, and how path queries speed up exploration of nested documents.