Back to Blog

How to Fix "Unexpected Token" in JSON: A Complete Debug Guide

Mar 31, 20266 min read

If you've ever called JSON.parse() and seen SyntaxError: Unexpected token < in JSON at position 0, you're in the right place. This is the single most searched JSON error on the internet — and it has a handful of well-known root causes, all of which are fixable in minutes.

What Does "Unexpected Token" Actually Mean?

The JSON parser reads your string one character at a time. When it encounters a character it didn't expect at that position — an angle bracket <, a single quote ', a letter, or even a space in the wrong place — it throws a SyntaxError and stops immediately. The position number in the error (e.g. at position 0) tells you exactly where parsing broke.

The 5 Most Common Causes

1. The API returned HTML instead of JSON

This is the #1 cause of Unexpected token < at position 0. Your server returned an HTML error page (404, 500, or a login redirect) instead of the JSON your code expected. The very first character of HTML is < — which is not valid JSON.

How to diagnose it
const res = await fetch('/api/data');
// Always check status BEFORE parsing:
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const data = await res.json(); // now safe

Quick check

Open your browser DevTools → Network tab → click the failing request → Preview tab. If you see HTML, the server is the problem, not your JSON parsing code.

2. Single quotes instead of double quotes

JSON requires double quotes (") for both keys and string values. Single quotes are valid JavaScript but are not valid JSON.

Invalid JSON
{ 'name': 'Alice' }   // ❌ single quotes
Valid JSON
{ "name": "Alice" }   // ✅ double quotes

3. Trailing commas

JavaScript objects and arrays allow trailing commas, but JSON strictly forbids them. Hand-edited config files are the most common offender.

Invalid JSON
{
  "host": "localhost",
  "port": 3000,   // ❌ trailing comma
}

4. Comments in JSON

Unlike JavaScript or YAML, JSON has no comment syntax. Any // or /* */ comment will cause the parser to fail immediately.

5. Byte Order Mark (BOM) in the file

Files saved with a UTF-8 BOM start with invisible bytes that look like garbage to the JSON parser. This is common with files created in Windows Notepad. The fix is to re-save the file as UTF-8 without BOM.

How to Fix It Fast

  • Paste your JSON into our JSON Validator — it pinpoints the exact line and character causing the error.
  • Check the Network tab in DevTools if the JSON comes from an API call.
  • Log the raw string before parsing: console.log(typeof raw, raw.slice(0, 50)) to confirm you're actually getting JSON.
  • Use optional chaining and try/catch around JSON.parse() to handle errors gracefully in production.
Defensive JSON.parse() pattern
function safeParseJSON(str) {
  try {
    return { data: JSON.parse(str), error: null };
  } catch (err) {
    console.error('JSON parse failed:', err.message, '\nInput:', str.slice(0, 100));
    return { data: null, error: err.message };
  }
}

Fix JSON errors instantly

Paste your broken JSON and our validator will highlight the exact problem: