Fix JSON Error: Unexpected Token
The 'Unexpected token' error is one of the most common JSON parsing errors. Here's exactly what causes it and how to fix every variation.
The Error Message
When you try to parse invalid JSON, your parser throws something like this:
SyntaxError: Unexpected token ',' at position 42
SyntaxError: Expected property name or '}' in JSON at position 15
JSON.parse: unexpected character at line 2 column 5The wording varies between browsers and runtimes, but the cause is always the same: the parser hit a character it wasn't expecting at that point in the JSON structure.
What Causes This Error
Four issues account for almost every "unexpected token" error you'll encounter in the wild.
1. Trailing Commas
JavaScript lets you leave a comma after the last item in an array or object. JSON does not. This is probably the single most common cause of the error.
// ❌ Broken — trailing comma after "blue"
{
"colors": ["red", "green", "blue",]
}// ✅ Fixed — removed the trailing comma
{
"colors": ["red", "green", "blue"]
}2. Comments in JSON
JSON does not support comments of any kind — not // single-line comments and not /* */ block comments. If you need configuration with comments, consider JSONC or JSON5, but standard JSON parsers will reject them.
// ❌ Broken — comments aren't valid JSON
{
// database settings
"host": "localhost",
"port": 5432 /* default postgres port */
}// ✅ Fixed — comments removed
{
"host": "localhost",
"port": 5432
}3. Single Quotes Instead of Double Quotes
JSON requires double quotes for all strings and keys. Single quotes, backticks, and unquoted strings are all invalid.
// ❌ Broken — single quotes
{
'name': 'Alice',
'age': 30
}// ✅ Fixed — double quotes
{
"name": "Alice",
"age": 30
}4. Unquoted Object Keys
In JavaScript you can write { name: "Alice" }, but JSON requires every key to be a double-quoted string.
// ❌ Broken — unquoted keys
{
name: "Alice",
age: 30,
active: true
}// ✅ Fixed — keys wrapped in double quotes
{
"name": "Alice",
"age": 30,
"active": true
}How to Fix It Step by Step
- Read the position number. The error message usually tells you the exact character index or line/column. Jump straight to that location.
- Look at the character just before the position. The parser stopped at the unexpected token, so the actual problem is often right before it — a trailing comma, a missing quote, or a stray comment.
- Check for the four common causes above. In most cases, the fix is removing a comma, deleting a comment, or switching single quotes to double quotes.
- Validate the whole document. After fixing the first error, run the JSON through a validator to catch any remaining issues.
Prevention Tips
- Use a JSON-aware editor or IDE extension that highlights syntax errors in real time.
- If you're generating JSON from code, always use your language's built-in serializer (like
JSON.stringify()) rather than building JSON strings by hand. - When copying JSON from JavaScript source code, remember to convert single quotes to double quotes and remove trailing commas.
- Set up a CI lint step that validates any committed JSON files automatically.
Fix it automatically: Paste your broken JSON into our JSON Repair tool — it handles this error and dozens more. Or validate your JSON first with our JSON Validator.