Ffile2fix
Sign in Get started

Fix "SyntaxError: Unexpected token" in JSON.parse

SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON

JSON.parse or response.json() hit a character that is not allowed at that point in JSON. A '<' at position 0 almost always means the server returned an HTML page (an error, login or 404 page) instead of JSON; other tokens point to syntax problems such as single quotes, trailing commas or parsing a value that is already an object.

Also appears as: SyntaxError: Unexpected token < in JSON at position 0 · SyntaxError: Unexpected token 'o', "[object Object]" is not valid JSON · SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data · SyntaxError: Expected property name or '}' in JSON at position 1

Common causes

  • The API returned an HTML error page, 404 page or login redirect instead of JSON
  • A PHP warning or notice was printed before the JSON output
  • Calling JSON.parse on something that is already an object, which becomes "[object Object]"
  • Single quotes, unquoted keys, trailing commas or comments, none of which are valid JSON
  • A UTF-8 byte order mark (BOM) or stray whitespace character at the start of the file
  • Parsing the string "undefined" from an empty localStorage value or variable

How to fix it

  1. Look at the raw response. Open DevTools > Network, click the request and view the Response tab, or log await res.text() before parsing. If it starts with <!DOCTYPE or <br />, the server sent HTML.
  2. Check the status and URL. Check res.ok and res.status before calling res.json(). A 404 or 500 usually returns HTML; fix the endpoint path or the server error first.
  3. Fix PHP output before the JSON. Look for 'Warning:' or 'Deprecated:' text in front of the JSON. Fix the warning, set display_errors=Off in production, and send header('Content-Type: application/json') followed by echo json_encode($data).
  4. Do not parse objects twice. If the value is already an object (for example from res.json() or axios response.data), use it directly. Unexpected token 'o' with "[object Object]" means it was parsed twice.
  5. Correct the JSON syntax. Use double quotes for all keys and strings, remove trailing commas and comments, and save the file as UTF-8 without BOM. A validator shows the exact line and column.

JavaScript

const res = await fetch('/api/items');
const text = await res.text();
if (!res.ok) throw new Error(`HTTP ${res.status}: ${text.slice(0, 200)}`);
let data;
try {
  data = JSON.parse(text);
} catch (e) {
  console.error('Not JSON:', text.slice(0, 200));
  throw e;
}

How to stop it happening again

  • Return JSON error bodies with the right status code from your API, not HTML pages
  • Turn off display_errors in production and log PHP errors instead
  • Generate JSON with JSON.stringify or json_encode rather than building strings by hand
  • Check response.ok and the Content-Type header before parsing

Frequently asked questions

Why is the unexpected token '<' at position 0?

The first character of the response is '<', which starts an HTML tag. The server returned a web page, usually an error, 404 or login page, so look at the request URL and status code.

Why does the message look different in Chrome and Firefox?

Each JavaScript engine writes its own text. Newer Chrome and Node versions quote the start of the bad input, while Firefox reports the line and column; the cause is the same.

Is a JSON file with comments valid?

No. Standard JSON does not allow comments or trailing commas. Formats like JSONC or JSON5 accept them, but JSON.parse does not.