Unexpected Token in JSON at Position 0: How to Fix It

If you landed here, you probably ran something like JSON.parse(someValue) and your console threw this at you:

Uncaught SyntaxError: Unexpected token < in JSON at position 0

or

Uncaught SyntaxError: Unexpected token u in JSON at position 0

Good news: this error almost always comes from the same root cause. Position 0 is the key detail here. It tells you the problem is right at the first character of the string you tried to parse. In other words, JSON.parse() didn’t even get to interpret anything, it failed immediately.

Why this happens

JSON.parse() expects a string that starts with {, [, ", a number, true, false, or null. If the first character is anything else, it throws this error. In practice, this usually comes down to one of three things.

1. You’re parsing HTML, not JSON

By far the most common case. This usually happens when you make a fetch() call expecting a JSON response from an API, but the API returns an HTML error page instead (like a 404 or 500 page).

// javascript
fetch('/api/data')
  .then(res => res.json()) // breaks here if the response is HTML

Notice the < in the error message, Unexpected token <. That’s literally the < from <!DOCTYPE html> or <html>. The server returned an error page instead of the JSON you expected.

How to confirm it: instead of calling .json() right away, log the raw response as text first.

// javascript
fetch('/api/data')
  .then(res => res.text())
  .then(text => console.log(text)) // see what actually came back

If <!DOCTYPE html> shows up in the console, the problem isn’t your parsing logic. It’s the route or endpoint you’re calling (wrong URL, a route that doesn’t exist, or the server returning an error).

2. You’re trying to parse undefined

The error Unexpected token u in JSON at position 0 is very specific: that u comes from undefined. It happens when you call JSON.parse(undefined), usually because a variable that was supposed to hold a JSON string is empty.

// javascript
const data = localStorage.getItem('key_that_does_not_exist');
JSON.parse(data); // data is null here, but test for other cases too

An even more common example: an API or state variable that hasn’t loaded yet.

// javascript
let apiResponse; // not populated yet
JSON.parse(apiResponse); // undefined -> error

How to fix it: always validate before parsing.

// javascript
if (data) {
  const parsed = JSON.parse(data);
}

3. Empty string or already an object

Sometimes the value is already a JavaScript object (not a string), and you’re trying to parse it again unnecessarily, or the string is empty.

// javascript
const obj = { name: "test" };
JSON.parse(obj); // error: obj is already an object, no parsing needed

If the value is already an object, you don’t need JSON.parse() at all. Just use it directly.

Quick checklist

  • Confirm the response is actually JSON (log it as text before parsing)
  • Check that the variable isn’t undefined or empty before parsing
  • Make sure you’re not trying to parse something that’s already an object
  • If it’s an API, check the URL/endpoint. Error responses (404, 500) usually come back as HTML

Testing it quickly

If you want to confirm a string is valid JSON before dropping it into your code, you can paste it into JSON Formatter & Validator. It points out exactly where the problem is, no need to run anything in the console.