What Does “SyntaxError: Unexpected token in JSON” Mean?
The error SyntaxError: Unexpected token [character] in JSON at position X simply means you passed something to JSON.parse() that is not a valid JSON string.
The V8 JavaScript engine stops the parser the exact moment it hits a character that breaks the JSON rules. By looking at the specific character (the “token”) and its position, we can quickly figure out exactly why your application crashed.
Need an instant fix? Paste your broken payload into our Free JSON Validator. It will highlight the exact line and position of the unexpected token so you don’t have to guess.
1. Fix: Unexpected token u in JSON at position 0
This is extremely common in React, Next.js, and vanilla JavaScript when dealing with local storage or uninitialized variables.
The Cause
The letter u at position 0 almost always comes from trying to parse the actual word undefined.
Note that the string "undefined" is not valid JSON. (Valid JSON can only be Objects {}, Arrays [], Strings "", Numbers, true, false, or null).
// This throws "Unexpected token u"
const myData = undefined;
JSON.parse(myData);
// This often happens with LocalStorage
const savedInfo = localStorage.getItem('key_that_does_not_exist'); // returns null or undefined
JSON.parse(savedInfo);
The Fix
Always provide a fallback string (like an empty object "{}" or null) before parsing to ensure JSON.parse receives valid syntax.
const savedInfo = localStorage.getItem('user_settings');
// Fallback to an empty JSON object string if null/undefined
const parsedInfo = JSON.parse(savedInfo || "{}");
2. Fix: Unexpected token o in JSON at position 1
This error (json parse unexpected token o) confuses many developers because they swear they are passing an object to the parser.
The Cause: “Double Parsing”
You are trying to parse something that is already a JavaScript Object, not a string.
When you pass a raw Object { id: 1 } into JSON.parse(), JavaScript first implicitly converts it to a string using the .toString() method.
The string representation of a JS object is literally: "[object Object]".
When JSON.parse("[object Object]") runs, it reads the [ fine, but the letter o at position 1 breaks the JSON specification.
const data = { name: "IZHubs" }; // Already an object!
JSON.parse(data); // "Unexpected token o in JSON at position 1"
The Fix
Ensure you only parse strings. If you aren’t sure what data type you are receiving, check it first:
let parsedData;
if (typeof data === 'string') {
parsedData = JSON.parse(data);
} else {
parsedData = data; // It's already an object!
}
3. Fix: Unexpected token < (or DOCTYPE is not valid JSON)
Whether you see unexpected token < in json at position 0, unexpected token doctype is not valid json, or unrecognized token '<', they all mean the exact same thing: Your server sent you HTML instead of JSON.
The Cause
This happens 90% of the time when using fetch() to call an API. If the API crashes or the URL is wrong, the server returns a 404 Not Found or 500 Internal Error HTML page.
HTML pages start with <!DOCTYPE html> or <html>. The < at position 0 immediately breaks the JSON parser.
The Fix
Never blindly call .json() on a network response. Always check the response.ok property first.
const response = await fetch('/api/data');
if (!response.ok) {
// If the server failed, read as text so we can see the HTML error page.
const errorText = await response.text();
throw new Error(`API Failed! Received HTML/Text instead of JSON: ${errorText}`);
}
// Only parse if the response was successful (HTTP 200-299)
const data = await response.json();
Common Edge Cases
Unexpected token ' (Single Quotes)
JSON specifications strictly require double quotes (") for keys and string values. If you try to parse { 'name': 'John' }, it will throw an error at the first single quote.
If you received data with single quotes from an LLM like ChatGPT, you can use our JSON Formatter to automatically convert single quotes to formatting-compliant double quotes.
Hidden Characters (BOM)
Sometimes text editors add an invisible Byte Order Mark (BOM) to the beginning of a file. If your code breaks at position 0 but you see a valid { character, your file might be encoded as UTF-8 with BOM. Resave the file as plain UTF-8.
Summary Summary
Whenever you face a JSON syntax error, look closely at the “Token” it mentions:
- Token
u? You’re parsingundefined. - Token
o? You’re parsing an object instead of a string. - Token
<? You fetched an HTML error page.
Stop guessing where the error is. Paste your data into a JSON Validator to visualize the problem instantly.