How to Fix Invalid JSON: Common Errors and Solutions
Fix invalid JSON fast. A complete guide to the most common syntax errors — trailing commas, single quotes, unquoted keys, bad escapes, and truncated payloads — with examples and a reliable repair workflow.
Invalid JSON stops pipelines, breaks deployments, and crashes API clients with cryptic messages like 'Unexpected token' or 'Expecting property name'. The good news: almost every JSON error falls into a small set of recurring patterns. Once you can recognize them on sight, fixes take seconds instead of hours. This guide walks through each common error, shows the exact fix, and gives you a repeatable workflow for repairing JSON reliably.
Trailing commas
JSON does not allow a comma after the last property in an object or the last item in an array. This is the single most common error in hand-edited configs and in JSON generated by language models, because many programming languages (and JSON5) do permit trailing commas. Remove the comma that sits immediately before a closing } or ].
{ "a": 1, "b": 2, } <- invalid
{ "a": 1, "b": 2 } <- valid
[ 1, 2, 3, ] <- invalid
[ 1, 2, 3 ] <- validSingle quotes instead of double quotes
JavaScript and Python both allow single-quoted strings, but JSON requires double quotes for every string and every key. If you copied an object literal out of code, this is usually the first thing to fix. Replace each pair of single quotes with double quotes.
{ 'name': 'Ada' } <- invalid
{ "name": "Ada" } <- validUnquoted or missing keys
Every object key in JSON must be a double-quoted string. Bare keys like {name: "Ada"} are valid JavaScript but invalid JSON. This often appears together with single-quote errors when JSON is pasted from source code. Wrap every key in double quotes.
{ name: "Ada" } <- invalid
{ "name": "Ada" } <- validBad escapes and control characters
Strings can break JSON in subtler ways. A literal newline or tab inside a string must be escaped as \n or \t. A backslash must be doubled as \\, and a double quote inside a string must be escaped as \". Unescaped control characters and stray backslashes produce 'Bad control character' or 'Bad escaped character' errors. This is common when JSON is built by string concatenation instead of a proper serializer.
{ "path": "C:\Users\Ada" } <- invalid (lone backslashes)
{ "path": "C:\\Users\\Ada" } <- valid
{ "note": "line1
line2" } <- invalid (raw newline)
{ "note": "line1\nline2" } <- validWrong literals, comments, and trailing junk
Several smaller mistakes cause parse failures. JSON literals must be lowercase, so True, False, and None (Python-style) are invalid — use true, false, and null. Comments are not allowed in standard JSON, so // and /* */ must be removed. Numbers cannot have leading zeros, a leading plus sign, or values like NaN and Infinity. Finally, a document must contain exactly one top-level value — extra characters after the closing brace, such as a stray semicolon or a second object, will fail.
{ "ok": True, "x": NaN } // <- invalid
{ "ok": true, "x": 0 } <- valid (no comment, valid number)A reliable repair workflow
When you face a broken document, do not guess. Paste it into a JSON Validator to get the exact line and column of the first error, then fix errors one at a time from the top — later errors frequently disappear once the first is resolved, because the parser was misreading everything that followed. A JSON Editor with inline highlighting makes this even faster by flagging problems as you type.
For payloads that are mangled in predictable ways — trailing commas, single quotes, markdown fences from an LLM — a JSON Repair tool can apply the common fixes automatically, after which you re-validate. Everything runs in your browser on JsonDrift, so you can safely repair JSON that contains secrets or customer data without it leaving your machine.
Frequently asked questions
Why does my JSON say 'Unexpected token' or 'Unexpected end of input'?
'Unexpected token' means the parser hit a character it did not expect at that position — often a trailing comma, a single quote, or an unquoted key. 'Unexpected end of input' usually means the document is truncated or a bracket or brace was never closed. Start at the reported position and work outward.
How do I find which line has the error?
Paste the document into a JSON validator. It reports the exact line and column of the first syntax error, which is far faster than scanning by eye. Fix that error first, then re-validate to surface the next one.
Is there a tool that fixes JSON automatically?
Yes, for common mechanical errors. A JSON repair tool can strip trailing commas, convert single quotes to double quotes, quote bare keys, and remove markdown code fences. Always re-validate the result, because automatic repair cannot infer intent when data is genuinely ambiguous or truncated.
Can ChatGPT and other LLMs output valid JSON?
Often, but not reliably. Models add markdown fences, trailing commas, or comments, and they can truncate output at the token limit. Always validate model-generated JSON before using it in production, and prefer the provider's structured-output or JSON mode plus schema validation when available.
Why is my JSON valid in JavaScript but rejected by a JSON parser?
JavaScript object literals are more permissive than JSON. They allow single quotes, unquoted keys, trailing commas, comments, and values like undefined and functions — none of which are legal JSON. Convert the literal to strict JSON by quoting keys, using double quotes, and removing anything that is not a valid JSON value.
Related tools
Try these tools
JSON Validator
Validate JSON instantly with precise, human-readable error messages and line numbers.
JSON Repair & Fix
Fix invalid JSON: trailing commas, quotes, unquoted keys, and LLM markdown fences.
JSON Editor
Write, edit, format, minify, and validate JSON in a fast full-screen or embedded editor.
JSON Formatter & Beautifier
Format, prettify, and beautify messy JSON with configurable indentation and key sorting.