How to Find and Fix Invalid JSON
Most JSON failures come from a small set of syntax mistakes: single quotes, trailing commas, missing delimiters, invalid escapes, or extra text around the value.
Know the valid building blocks
A JSON document contains an object, array, string, number, true, false, or null. Object member names are strings in double quotes. Strings also use double quotes, and special characters inside them must be escaped. Comments, undefined, functions, and single-quoted strings are features of some programming languages—not valid JSON.
Read the first parser error first
Later errors are often consequences of the earliest missing comma, quote, bracket, or brace. Start at the reported character or line, then inspect immediately before it. A parser may point at the character where it finally became impossible to continue rather than the exact character that caused the mistake.
- Replace single quotes around keys and strings with double quotes.
- Remove a comma immediately before a closing brace or bracket.
- Escape a literal backslash as two backslashes inside a JSON string.
- Ensure braces and brackets close in the reverse order they opened.
Separate JSON from JavaScript
The JavaScript object literal {name: undefined} is not JSON: the key lacks double quotes and undefined is not a JSON value. A valid representation might be {"name": null}, but only if null accurately represents the data. Do not silently change a value merely to satisfy the parser; decide what the producer actually means.
Format only after validation
Pretty-printing can expose nesting and missing delimiters, but it should not rewrite invalid data without telling you. Validate first, correct the source, then format. If the JSON came from an application or API, fix the serializer that produced it so the error does not recur. Keep a copy of the original payload while debugging.
Primary sources and further reading
Last reviewed: September 15, 2026