← All guides

"Unexpected Token in JSON at Position 0" — What It Actually Means

SyntaxError: Unexpected token X in JSON at position 0 is JSON.parse telling you the very first character it read wasn't valid JSON. Position 0 is the important detail — this isn't a typo three levels deep into a large payload, it's the first character. That narrows down the cause a lot.

The most common real cause: you didn't get JSON back

By far the most frequent version of this is Unexpected token < in JSON at position 0. The < is the start of <!DOCTYPE html> or <html> — your code called JSON.parse on an HTML page, not JSON. This happens constantly with API calls: the request hit a 404 page, a 500 error page, or got redirected to an HTML login page, and the calling code parsed the response body as JSON without checking the status code or content type first.

const res = await fetch('/api/data')
const data = await res.json()   // throws here if the server actually sent back an HTML error page

// check first:
const res = await fetch('/api/data')
if (!res.ok) throw new Error(`Request failed: ${res.status}`)
const data = await res.json()

The other common one: parsing something that isn't a string

Unexpected token u in JSON at position 0 — the u is the start of the word undefined. This happens when a variable that's actually undefined gets coerced to the string "undefined" before being handed to JSON.parse, usually because a value that was supposed to be set (a cached response, a localStorage read that returned null) wasn't.

How to actually find it

Log the raw response text before you parse it, not after — once JSON.parse has thrown, the original string is gone from that stack frame. If what you paste into a JSON formatter also fails at position 0, paste exactly what you logged, not what you assumed the response should look like — the formatter here converts that character offset into an actual line number for you, which matters more once the bad character isn't at position 0 but somewhere deep in a large payload.

Try the JSON Formatter