SyntaxError: Unexpected token in JSON at position 0 - How to Fix It

JSON Nova Team

If you've spent any time working with JavaScript, Node.js, or React, you have almost certainly encountered this error:

Uncaught SyntaxError: Unexpected token 'o', "object Obj"... is not valid JSON
// or
SyntaxError: Unexpected token o in JSON at position 1

This error is incredibly common and frustrating because it doesn't tell you what is wrong with your code, only that JSON.parse() failed. In this guide, we'll break down exactly why this error happens and how to fix it permanently.

What Causes the "Unexpected Token" Error?

The error occurs when you pass a string to JSON.parse() that does not conform to the strict JSON specification.

When JavaScript tries to parse the string, it expects the very first character (position 0) to be a valid JSON opening character:

  • { for an object
  • [ for an array
  • " for a string

If it encounters anything else—like a letter, an HTML tag, or an undefined value—it immediately throws the SyntaxError.

1. You are parsing a JavaScript Object, not a JSON String

This is the most common mistake. JSON.parse() requires a String. If you pass a JavaScript object into it, JavaScript will implicitly convert that object to a string by calling .toString() on it.

When you call .toString() on an object, the result is the literal string "[object Object]".

const data = { name: "Alice" }; // This is an Object, not a JSON string!
const parsed = JSON.parse(data); 

// What actually happens:
// JSON.parse("[object Object]")
// 💥 SyntaxError: Unexpected token o in JSON at position 1

The Fix: Don't parse an object. It's already an object! Just use it directly.

2. You received HTML instead of JSON

If your API endpoint fails and returns a 500 or 404 error, the server often responds with an HTML error page instead of JSON.

fetch('/api/data')
  .then(res => res.json()) // 💥 Fails!

If the server sends back <!DOCTYPE html>, the JSON parser sees the < character at position 0 and immediately throws: SyntaxError: Unexpected token < in JSON at position 0.

The Fix: Always check the response status and content type before parsing.

const response = await fetch('/api/data');
if (!response.ok) {
  throw new Error(`HTTP error! status: ${response.status}`);
}
const contentType = response.headers.get("content-type");
if (contentType && contentType.indexOf("application/json") !== -1) {
  const data = await response.json();
} else {
  const text = await response.text();
  console.log("Expected JSON, but received:", text);
}

3. Hidden Characters and BOM

Sometimes, copying and pasting JSON from Word documents or Slack introduces hidden characters, such as the Byte Order Mark (BOM). These invisible characters sit at position 0, causing the parser to fail before it even reaches the opening bracket.

The Fix: Use a dedicated tool like the JSON Validator to instantly highlight hidden syntax errors, or run the payload through our JSON Cleaner to strip out invisible junk characters.

Conclusion

The Unexpected token in JSON error is simply JavaScript's way of saying, "You handed me something that isn't strict JSON."

The fastest way to debug this in production is to log the raw string using console.log(rawString), copy the output, and paste it into a strict JSON Formatter to instantly identify the anomaly.