How to Fix Invalid JSON: Common Syntax Errors
Quick Summary
Fix invalid JSON by changing single quotes to double quotes, deleting trailing commas, wrapping raw strings, and balancing mismatched braces.
Introduction
JSON is extremely strict. A single incorrect character will cause a SyntaxError and halt execution. In this practical guide, we will analyze the top four errors developers make when drafting JSON files and walk through how to solve them.
Error 1: Trailing Commas
The Mistake
{
"name": "Alex",
"role": "QA",
}
Why it fails: The comma after "QA" implies there is another key/value pair following it.
The Fix
Remove the trailing comma on the final element:
{
"name": "Alex",
"role": "QA"
}
Error 2: Single Quotes for Keys or Values
The Mistake
{
'id': 456,
'status': 'pending'
}
Why it fails: JSON specification requires double quotes (") for all keys and string values. Single quotes are syntactically invalid.
The Fix
Swap all single quotes with double quotes:
{
"id": 456,
"status": "pending"
}
Error 3: Unescaped Quotes Inside Strings
The Mistake
{
"quote": "She said "Hello" to me"
}
Why it fails: The nested double quotes around "Hello" close the string early, confusing the parser.
The Fix
Escape internal quotes using backslashes (\):
{
"quote": "She said \"Hello\" to me"
}
Error 4: Unwrapped String Keys
The Mistake
{
username: "admin"
}
Why it fails: Unlike standard JavaScript objects, JSON keys must be wrapped in double quotes.
The Fix
{
"username": "admin"
}
Frequently Asked Questions
Can I run a automatic tool to clean bad JSON?
Yes! Our JSON Editor can help visualize and identify error spots immediately so you can fix them interactively.
Need to work with JSON right now?
Check out our 100% private, fast, browser-based tools.