JSON looks simple—until one missing comma turns into:
SyntaxError: Unexpected token ... in JSON
I've run into JSON parsing errors plenty of times while working with APIs, configuration files, and frontend applications.
The good news is that most JSON errors come down to a handful of common mistakes.
Here are five of them and how to fix each one.
1. Missing Commas
One of the easiest mistakes to make is forgetting the comma between properties.
❌ Invalid JSON:
{
"name": "John"
"age": 30
}
There must be a comma after "John".
✅ Valid JSON:
{
"name": "John",
"age": 30
}
This gets harder to spot when you're dealing with large API responses or deeply nested objects.
2. Using Single Quotes
JavaScript allows strings using single quotes, but JSON does not.
❌ Invalid JSON:
{
'name': 'John',
'role': 'developer'
}
JSON requires double quotes around property names and string values.
✅ Valid JSON:
{
"name": "John",
"role": "developer"
}
This is an important distinction between a JavaScript object literal and actual JSON.
3. Trailing Commas
Trailing commas are another common source of parsing errors.
❌ Invalid JSON:
{
"name": "John",
"age": 30,
}
That final comma after 30 makes the JSON invalid.
✅ Valid JSON:
{
"name": "John",
"age": 30
}
The same rule applies to arrays.
❌ Invalid:
{
"skills": [
"JavaScript",
"React",
]
}
✅ Valid:
{
"skills": [
"JavaScript",
"React"
]
}
4. Unescaped Characters Inside Strings
Suppose your JSON contains this:
❌ Invalid JSON:
{
"message": "He said "Hello" to me."
}
The parser interprets the quote before Hello as the end of the string.
Escape quotes inside JSON strings using a backslash.
✅ Valid JSON:
{
"message": "He said \"Hello\" to me."
}
This problem often appears when JSON contains user-generated text, HTML, logs, or other embedded content.
5. Using JavaScript Values That JSON Doesn't Support
JSON supports only a limited set of value types:
- Strings
- Numbers
- Objects
- Arrays
- Booleans
null
Values such as undefined, functions, and NaN are not valid JSON values.
❌ Invalid JSON:
{
"name": "John",
"lastLogin": undefined
}
Use null when you intentionally need to represent an empty value.
✅ Valid JSON:
{
"name": "John",
"lastLogin": null
}
This distinction becomes especially important when serializing JavaScript data before sending it to an API.
🔍 A Quick Way to Debug JSON
When JSON parsing fails, I usually check these things first:
- Are all property names using double quotes?
- Are commas missing between properties?
- Is there a trailing comma?
- Are quotes inside strings escaped?
- Are there unsupported JavaScript values?
- Are all
{},[], and quotes properly closed?
For small objects, checking manually is easy.
For larger API responses, it becomes painful pretty quickly.
That's one of the reasons I built a browser-based JSON Formatter & Validator as part of CodeStrategists.
👉 https://www.codestrategists.com/tool/json-formatter
You can paste your JSON there to format it and quickly identify structural problems.
I also wrote a more detailed guide covering JSON parse errors and debugging techniques in the CodeStrategists Developer Academy:
📚 https://www.codestrategists.com/academy/json-parse-errors
Both resources are completely free to use.
💡 One More Useful Trick
When you're working directly in JavaScript, wrap JSON.parse() in a try...catch block instead of letting malformed data crash your code.
function parseJSON(value) {
try {
return JSON.parse(value);
} catch (error) {
console.error("Invalid JSON:", error);
return null;
}
}
For production applications, you may want more specific error handling, but this simple pattern is useful while debugging.
Final Thoughts
JSON parsing errors can look intimidating because the error message often points to a character position rather than clearly explaining the actual mistake.
But most of the time, the cause is surprisingly small:
A missing comma, an extra comma, incorrect quotes, an unescaped character, or an unsupported value.
Learning to recognize these patterns makes debugging JSON much faster.
I'm building CodeStrategists to make common developer tasks easier while also explaining the concepts behind them.
I'm building CodeStrategists to make common developer tasks easier while also explaining the concepts behind them.
What JSON error has wasted the most time for you? 😄
Top comments (0)