How to Fix Invalid JSON
Invalid JSON is frustrating — but every JSON syntax error has a clear pattern and a clear fix. This guide walks you through diagnosing the error, finding the line, and applying the right fix for every common case.
The 4-Step JSON Fix Process
Paste into Validator
Copy your broken JSON and paste it into the JSON Validator to get the exact error message and line number.
Read the Error
The error message tells you what went wrong. "Unexpected token ," usually means trailing comma. "Unexpected end" means a missing bracket.
Apply the Fix
Find your error type below and follow the step-by-step fix. Each section has an invalid/valid code pair.
Re-validate
Paste the fixed JSON back into the validator. If there are multiple errors, fix them one at a time from the top.
Jump to Fix
Trailing Comma
SyntaxError: Unexpected token }cancel Invalid JSON
{
"name": "Alice",
"age": 30,
}check_circle Fixed JSON
{
"name": "Alice",
"age": 30
}Fix Steps
- 1Find the last key-value pair before a closing } or ]
- 2Remove the comma after that last item
- 3Re-validate your JSON
Tip: Most text editors with JSON syntax highlighting will highlight trailing commas as errors.
Single Quotes Instead of Double Quotes
SyntaxError: Unexpected token 'cancel Invalid JSON
{
'name': 'Alice',
'age': 30
}check_circle Fixed JSON
{
"name": "Alice",
"age": 30
}Fix Steps
- 1Use Find & Replace to replace all ' with "
- 2Be careful not to replace apostrophes inside string values
- 3Validate the result
Tip: JSON requires double quotes everywhere. Single quotes are only valid in JavaScript, not JSON.
Unquoted Object Keys
SyntaxError: Unexpected token ncancel Invalid JSON
{
name: "Alice",
age: 30
}check_circle Fixed JSON
{
"name": "Alice",
"age": 30
}Fix Steps
- 1Find all object keys (words before the : colon)
- 2Wrap each key in double quotes
- 3Validate the result
Tip: In JavaScript, object keys are optional to quote. In JSON, they are always required.
undefined or NaN Values
SyntaxError: Unexpected token ucancel Invalid JSON
{
"value": undefined,
"score": NaN,
"limit": Infinity
}check_circle Fixed JSON
{
"value": null,
"score": null,
"limit": null
}Fix Steps
- 1Replace undefined with null
- 2Replace NaN with null or a valid number
- 3Replace Infinity with null or a large number
Tip: undefined, NaN, and Infinity are JavaScript values. JSON only supports null as a non-value.
Missing or Extra Bracket
SyntaxError: Unexpected end of JSON inputcancel Invalid JSON
{
"users": [
{"name": "Alice"},
{"name": "Bob"}
}check_circle Fixed JSON
{
"users": [
{"name": "Alice"},
{"name": "Bob"}
]
}Fix Steps
- 1Count opening { and [ brackets
- 2Count closing } and ] brackets — they must match
- 3Add or remove brackets to balance them
Tip: Use a JSON formatter — it will visually indent the structure and make missing brackets obvious.
Unescaped Special Characters in Strings
SyntaxError: Bad control character in string literalcancel Invalid JSON
{
"message": "line one
line two",
"path": "C:UsersAlice"
}check_circle Fixed JSON
{
"message": "line one\nline two",
"path": "C:\\Users\\Alice"
}Fix Steps
- 1Replace actual newlines inside strings with \n
- 2Replace backslashes with \\
- 3Replace tabs with \t
Tip: Control characters and backslashes must be escaped inside JSON strings. Use JSON.stringify() to safely serialize values.
Still Stuck?
If you've applied the fix but still get a parse error, there may be multiple errors in your JSON. Parsers typically report only the first error they encounter. Fix errors one at a time from the top of the file, re-validating after each fix.
For complex or large JSON, try the JSON Formatter — it will attempt to pretty-print your JSON and may make structural problems visually obvious. The JSON Viewer shows JSON as a collapsible tree which makes missing brackets easy to spot.
FAQ – Fixing Invalid JSON
How do I fix a JSON syntax error?expand_more
Paste your JSON into a JSON validator to get the exact error message and line number. Then fix the specific problem — common fixes include removing trailing commas, replacing single quotes with double quotes, quoting object keys, and removing comments.
How do I find invalid JSON in a large file?expand_more
Use a JSON validator or formatter tool — it will pinpoint the exact line number of the first syntax error. For very large files, try splitting the JSON into smaller chunks to isolate the problem section.
Can I fix JSON automatically?expand_more
Some JSON formatters can auto-fix simple issues like trailing commas and whitespace. However, most syntax errors require manual fixes because the parser cannot guess your intent — for example, it cannot know whether a missing comma or a missing closing bracket is the real problem.
What does "Unexpected token" mean in a JSON error?expand_more
An "Unexpected token" error means the JSON parser encountered a character it didn't expect at that position. This is usually caused by a trailing comma, a single quote instead of a double quote, an unquoted key, or a comment inside the JSON.
Part of the JSON Toolkit
Explore All JSON Tools
Free online tools for every JSON task — format, validate, convert, compare, and more.
Comments in JSON
SyntaxError: Unexpected token /cancel Invalid JSON
{ "name": "Alice" // the user // "age": 30 }check_circle Fixed JSON
{ "name": "Alice" }Fix Steps
Tip: JSON has no comment syntax by design. Use JSON5 or JSONC if you need comments during development.