Solving "Unexpected token in JSON" and common JSON Parse Errors: A Complete Guide
JSON (JavaScript Object Notation) is the backbone of modern web communication. Whether you are building a REST API, configuring a server, or storing application state, JSON is everywhere. However, its strict syntax often leads to frustrating errors like SyntaxError: Unexpected token, JSON.parse: unexpected character, or simply invalid JSON format.
In this guide, we will break down the most common JSON parsing errors, explain why they happen, and show you exactly how to fix them.
1. Common JSON Error Messages
When a parser (like JSON.parse() in JavaScript) encounters invalid JSON, it throws an error. Depending on the environment, you might see:
- Browser (Chrome/V8):
SyntaxError: Unexpected token ' in JSON at position 0 - Firefox:
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data - Node.js:
SyntaxError: Unexpected token ... in JSON at position ... - Python:
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes
The key information is usually the character it didn't expect and the position (or line/column) where it happened.
2. Top Causes and Solutions
2.1 Single Quotes Instead of Double Quotes
JSON strictly requires double quotes (") for both keys and string values. Single quotes (') are valid in JavaScript objects but invalid in JSON strings.
The Error:
{
'name': 'Tool3M'
}
The Solution: Replace all single quotes with double quotes.
{
"name": "Tool3M"
}
2.2 Trailing Commas
Unlike modern JavaScript, JSON does not allow a comma after the last item in an object or array.
The Error:
{
"id": 1,
"status": "active",
}
The Solution: Remove the comma after the last property.
{
"id": 1,
"status": "active"
}
2.3 Unquoted Keys
In JSON, keys must always be strings enclosed in double quotes.
The Error:
{
id: 123
}
The Solution: Wrap keys in double quotes.
{
"id": 123
}
2.4 Comments in JSON
The JSON standard does not support comments (// or /* */). While some tools (like VS Code for tsconfig.json) allow them, standard JSON.parse() will fail.
The Error:
{
"id": 1 // This is a comment
}
The Solution:
Remove all comments before parsing. If you must have metadata, use a property like "__comment": "text".
2.5 Special Values: NaN, Infinity, and Undefined
JSON does not support NaN, Infinity, -Infinity, or undefined. These are JavaScript-specific values.
The Error:
{
"value": NaN,
"expired": undefined
}
The Solution:
Use null or a placeholder string/number instead.
{
"value": null,
"expired": null
}
2.6 Control Characters and Newlines
Strings in JSON cannot contain raw control characters (like tabs or actual newlines) unless they are escaped.
The Error:
{
"description": "This is a
multi-line string"
}
The Solution:
Use \n for newlines and \t for tabs.
{
"description": "This is a \nmulti-line string"
}
3. Advanced Troubleshooting
3.1 BOM (Byte Order Mark)
Sometimes a file looks perfect but still throws a SyntaxError at position 0. This is often caused by a hidden UTF-8 BOM character at the start of the file.
Solution: Re-save the file as "UTF-8 without BOM".
3.2 Improperly Escaped Characters
If your string contains a backslash (\) or a double quote ("), they must be escaped.
- Correct:
"path": "C:\\Windows\\System32" - Correct:
"quote": "He said \"Hello\""
4. Prevention Measures
- Use a Linter: Use tools like ESLint with the
jsonc-parseror VS Code's built-in JSON validation. - Automated Formatting: Always run your JSON through a formatter before saving it to a config file.
- Schema Validation: Use JSON Schema to define the structure of your data and validate it programmatically.
- Try-Catch: When parsing dynamic data, always wrap
JSON.parse()in atry...catchblock to handle errors gracefully.
try {
const data = JSON.parse(userInput);
} catch (e) {
console.error("Failed to parse JSON:", e.message);
// Show a friendly error to the user
}
5. FAQ: Frequently Asked Questions
Q: Why am I getting "Unexpected token < in JSON at position 0"?
A: This almost always means your code expected JSON but received HTML. This typically happens when an API call fails and the server returns an HTML error page (like a 404 or 500 page) instead of the expected JSON response.
Q: Can I use hexadecimal numbers in JSON?
A: No. JSON only supports standard decimal numbers. 0xFF will cause a parse error. Use 255 instead.
Q: How do I handle large JSON files that crash my parser?
A: For very large files, use a "streaming" parser like JSONStream (Node.js) or ijson (Python), which processes the file piece by piece instead of loading it all into memory.
6. Quick Check Tool
If you're struggling to find that one missing comma or misplaced quote, use our JSON Formatter & Validator. It will:
- Instantly highlight syntax errors.
- Fix common issues like single quotes or trailing commas.
- Format your JSON to make it human-readable.