The Most Common JSON Error, by a Wide Margin
SyntaxError: Unexpected token < in JSON at position 0
SyntaxError: Unexpected token } in JSON at position 47
SyntaxError: Unexpected non-whitespace character after JSON at position 128
If you write JavaScript that talks to an API, you will see some variant of this error. It comes from JSON.parse() encountering a character it didn't expect at a specific position in the string — and while the message is precise about where, it says nothing about why. Here's every real-world cause, in the order you should actually check them.
Cause 1: You Parsed HTML, Not JSON (the #1 cause of position 0)
SyntaxError: Unexpected token < in JSON at position 0
A < at position 0 is the single most common version of this error, and it's almost never actually a JSON formatting problem. It means your code called JSON.parse() on a string that starts with <!DOCTYPE html> or <html> — an actual HTML page, not JSON at all.
This happens when:
- The API returned a 404 or 500 error page (many servers serve an HTML error page by default, even for API routes)
- You hit a login redirect — your session expired and the server sent back an HTML login page instead of the JSON you expected
- The API is temporarily down and a load balancer or CDN's maintenance page intercepted the request
Fix: don't call .json() or JSON.parse() blindly — check the response status first:
const res = await fetch(url);
if (!res.ok) {
throw new Error(`Request failed: ${res.status} ${res.statusText}`);
}
const data = await res.json();
Cause 2: Trailing Commas
{
"name": "Jane",
"age": 30,
}
That comma after 30 is invalid JSON — it throws Unexpected token } in JSON. This trips people up specifically because a trailing comma is completely valid in a JavaScript object literal. Copy a JS object out of your code and paste it somewhere expecting real JSON, and this is exactly what happens.
Fix: remove the trailing comma. If you're hand-editing JSON often, use an editor with JSON linting enabled — it'll flag this before you ever run the code.
Cause 3: Single Quotes Instead of Double Quotes
{ 'name': 'Jane' }
JSON requires double quotes, for both keys and string values, with no exceptions. Single quotes are valid JavaScript, not valid JSON.
Fix: double quotes only. If you're generating JSON via string concatenation, use JSON.stringify() instead of hand-building the string — it handles quoting correctly by construction.