Unexpected Token in JSON: What It Means and How to Fix It
Your code calls JSON.parse() or response.json() and dies with Unexpected token. The message looks vague, but it is precise about one thing: the text it was given is not valid JSON, and it stopped at the character it names. Finding out why takes a minute once you know where to look.
Step 1: look at what you actually received
Before you change any code, print the raw text. Most parse errors come from the input, not from the parser.
const res = await fetch(url);
const text = await res.text();
console.log(res.status, res.headers.get('content-type'));
console.log(text.slice(0, 200));
const data = JSON.parse(text);
Reading the text first lets you see the status code, the content type and the first characters of the body. Then paste the text into the JSON Formatter, which reports the line and column of the first problem and suggests a fix.
What the message means
Wording differs between browsers and versions. Older engines say Unexpected token < in JSON at position 0, while newer ones show the start of the text, as in Unexpected token '<', "<!DOCTYPE "... is not valid JSON. The causes are the same.
| Error | Usual cause | Fix |
|---|---|---|
Unexpected token '<' at position 0 | The server returned HTML, such as a 404 page, a login page, a proxy error or a framework error screen | Check the URL, status and content type. See below. |
Unexpected end of JSON input | The body was empty or cut off, for example a 204 response, a timeout or a truncated file | Check for an empty body before parsing, and check the whole text arrived. |
Unexpected token 'u' or "undefined" is not valid JSON | You passed the value undefined, which becomes the text “undefined” | Find where the variable was never set, such as a missing localStorage key. |
Unexpected token 'o' or [object Object] | You parsed something that was already an object | Remove the extra JSON.parse(). response.json() already parses. |
Error about a trailing comma, or Expected double-quoted property name | A comma after the last item | Delete the comma. |
Unexpected token ''' or Expected property name or '}' | Single quotes or keys without quotes, which JavaScript allows but JSON does not | Use double quotes around keys and strings. |
| Error at position 0 with nothing visible | A byte order mark or another invisible character at the start of the file | Save the file as UTF-8 without BOM, or strip the first character. |
The most common one: an HTML response
A < at position 0 almost always means the start of <!DOCTYPE html> or <html>. Your request did get an answer, but not from the API you meant to reach. The usual reasons are:
- Wrong URL or route. A typo or a missing
/apiprefix gives a 404 page. - An expired session. The server redirects to a login page, and fetch follows it.
- A server error. A 500 error page is HTML even when successful responses are JSON.
- A dev server fallback. Some front-end servers return
index.htmlfor any unknown path, including a mistyped API path. - A proxy or captive portal. Corporate networks and public Wi-Fi can inject their own HTML page.
A safer pattern checks before it parses:
const res = await fetch(url);
const type = res.headers.get('content-type') || '';
if (!res.ok || !type.includes('application/json')) {
const body = await res.text();
throw new Error(`Expected JSON, got ${res.status} ${type}: ${body.slice(0, 100)}`);
}
const data = await res.json();
Now the error message tells you what the server said, instead of complaining about a character.
When the JSON was written by hand or copied
JSON looks like a JavaScript object but is stricter. Text copied from code, logs, chat tools or a document often fails for the same few reasons: trailing commas, single quotes, comments, unquoted keys, Python values such as True and None, or curly quotes added by a word processor. The JSON Formatter names each of these. With Auto-fix switched on it repairs the ones where the intent is clear and tells you what it changed, so you can check before you use the result.
It will not guess when the text is cut off or the brackets do not match. Those need the original data.
A related trap: numbers that parse but change
Valid JSON can still lose data. JavaScript stores every number as a 64-bit float, so an ID such as 12345678901234567890 parses to a slightly different number. If you handle long IDs, ask the API to send them as strings. The formatter on this site only rewrites whitespace, so it can check and tidy your JSON without altering the numbers.
Checklist
- Log the status code, content type and first 200 characters of the raw text.
- If it starts with
<, fix the request, the URL or the login, not the parser. - If it is empty or cut off, check for a 204 response, a timeout or a partial file.
- If it is text you wrote or copied, paste it into the formatter to find the exact line and column.
- Do not call
JSON.parse()on a value that is already an object.
Related: how to open a JSON file and JSON vs CSV.