How to Fix "Unexpected Token" JSON Parse Errors
The Unexpected token error in JSON.parse() is the most common JSON error in JavaScript and API development. It means your JSON string contains invalid syntax that the parser cannot understand. Here are the 7 most common causes and instant fixes.
Table of Contents
The 7 Most Common Causes
JSON (JavaScript Object Notation) is a strict data format. While it looks like JavaScript object literals, it has significantly more constraints. Even a single misplaced character can crash your application.
1. Trailing commas after the last property
This is the number one cause of JSON errors. JavaScript arrays and objects allow trailing commas, but JSON strictly forbids them.
Invalid JSON:
{
"name": "FmtDev",
"tools": ["JSON", "JWT"],
}
Fixed Version:
{
"name": "FmtDev",
"tools": ["JSON", "JWT"]
}
2. Single quotes instead of double quotes
In JavaScript, 'single quotes' and "double quotes" are interchangeable. In JSON, only double quotes are valid for strings and property names.
Invalid JSON:
{
'status': 'error'
}
Fixed Version:
{
"status": "error"
}
3. Unquoted property names
In a standard JavaScript object, you can omit quotes around keys if they don't contain special characters. In JSON, every key must be wrapped in double quotes.
Invalid JSON:
{
id: 101,
active: true
}
Fixed Version:
{
"id": 101,
"active": true
}
4. Comments in JSON
Developers often try to document their JSON files using // or /* */. This is not supported by the JSON specification (RFC 8259).
The Breakdown: If you need comments, you are likely looking for JSONC (JSON with Comments), which is used in VS Code configurations. However, standard parsers like JSON.parse() will throw an error if they encounter a forward slash.
5. Undefined or NaN values
JSON only supports specific data types: String, Number, Object, Array, Boolean, and null.
The Problem: If your backend accidentally sends undefined, NaN, or Infinity, the parser will fail. These values do not exist in the JSON spec.
- Fix: Replace
undefinedorNaNwithnull, or omit the property entirely.
6. BOM (Byte Order Mark) character at the start
A Byte Order Mark (BOM) is an invisible character (\uFEFF) that some text editors (especially on Windows) insert at the beginning of a file to indicate UTF-16 encoding.
The Fix: If you are reading a JSON file from disk and getting a SyntaxError: Unexpected token ... in JSON at position 0, you may need to strip the BOM before parsing.
const cleanedData = fs.readFileSync('data.json', 'utf8').replace(/^\uFEFF/, '');
JSON.parse(cleanedData);
7. HTML response instead of JSON (The "Position 0" Error)
If you see Unexpected token < in JSON at position 0, it almost always means your API request failed and returned an HTML error page (like a 404 or 500 page from Nginx/Apache) instead of the expected JSON.
The Fix: Check the Content-Type header of the response. If it says text/html, you aren't parsing JSON; you're parsing a webpage.
How to Debug JSON Errors Instantly
The manual "staring at the screen" method is slow and error-prone. Instead, use a specialized validator.
Why Line Numbers Matter
A professional JSON validator will tell you exactly where the error is:
Uncaught SyntaxError: Unexpected token 'j', ... "tools": [json... is not valid JSON at line 3, column 12
Privacy Warning
Never paste sensitive configuration files or customer data into public online formatters. Many of these tools log your inputs for "debugging" or tracking purposes.
The Local Solution
Use our Local JSON Formatter & Validator. It runs 100% in your browser memory via JavaScript. Your data is formatted and validated instantly without ever being sent to a server.
JSON Parse Errors by Language
Different languages have different ways of shouting at you when the JSON is broken.
JavaScript: JSON.parse()
// Error: SyntaxError: Unexpected token n in JSON at position 1
try {
JSON.parse("{name: 'test'}");
} catch (e) {
console.error(e.message);
}
Python: json.loads()
# Error: json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
import json
json.loads("{'key': 'value'}")
Java (Jackson/Gson)
- Jackson:
com.fasterxml.jackson.core.JsonParseException: Unexpected character ('s' (code 115)): was expecting double-quote to start field name - Gson:
com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 2
Go: json.Unmarshal
// Error: invalid character 'a' looking for beginning of object key string
err := json.Unmarshal(data, &v)
FAQ
What does "Unexpected token < in JSON at position 0" mean?
Your API returned HTML instead of JSON. The < is the start of an HTML tag (usually <!DOCTYPE html>). This happens when your URL is wrong (404), the server crashed (500), or you hit a login redirect page. Check the URL and the response status code.
Why does JSON not allow trailing commas?
The JSON specification (RFC 8259) was designed to be a strict, minimal data interchange format that is easy for machines to parse. By prohibiting trailing commas, the spec avoids ambiguity in parsing logic and ensures absolute consistency across all programming languages.
How can I validate JSON without sending it to a server?
Always use a local tool. FmtDev's JSON Formatter is built for privacy. It processes your data entirely on the client-side, making it safe for PII, API keys, and internal system logs.
Conclusion
Fixing JSON errors is usually a matter of checking your quotes and commas. However, when working with APIs, the error is often a sign of a deeper networking issue. By following the checklist above and using local-first validation tools, you can resolve "Unexpected token" errors in seconds.