SyntaxError: Unexpected token < in JSON at position 0: what it means and how to fix it

TL;DR: Validate locally, fix the first real error, validate again (no upload).

Troubleshoot SyntaxError: Unexpected token < in JSON at position 0 quickly and validate JSON locally (no upload).

What the error means

SyntaxError: Unexpected token < in JSON at position 0 means the parser expected valid JSON but encountered a character/token that cannot appear there. In practice, it usually means the input is not JSON (or not strict JSON), or it is incomplete.

Most common real-world causes

  • Your API returned HTML (login page, 404/500 error page), but you tried to parse it as JSON.
  • You hit the wrong URL (missing /api/, typo, or a CDN route serving HTML).
  • A backend error handler returns an HTML template by default (instead of JSON).
  • Auth/CORS redirects: the response body is an HTML redirect/SSO page.
  • You parse before checking res.ok and Content-Type.

Fast debugging steps

  • Print the first 200 characters of the response body before parsing.
  • Check HTTP status code and Content-Type; JSON is usually application/json.
  • If the body starts with <!doctype or <html, fix endpoint/error handling first.
  • Only call res.json() when you are sure the response is JSON.

Code example (node)

const res = await fetch(url);
const text = await res.text();

// Debug safely (avoid logging secrets in production)
console.log(text.slice(0, 200));

if (!res.ok) throw new Error(`HTTP ${res.status}`);
if (!(res.headers.get('content-type') || '').includes('application/json')) {
  throw new Error('Expected JSON but got a different Content-Type');
}

const data = JSON.parse(text);

Fix without uploading data

If the JSON contains sensitive data, validate and fix it locally. No Upload Tools runs 100% in your browser.

Workflow: validate -> fix the first error -> validate again -> export/convert.

FAQ

Does the exact token matter? Yes. The token often hints at the root cause: < is usually HTML, u is often undefined, and / often points to comments.

Should I just regex-fix JSON? Avoid blind regex edits. Validate after each change so you know what you fixed and what broke.

Privacy & Security
All processing happens locally in your browser. Files are never uploaded.