{}JSON FYI

JSON parse error at position 0 — causes and fixes

Why JSON parsers fail immediately at byte 0 — empty string, BOM, HTML error page, undefined — and the exact fix for each.

·5 min read

A parse error at position 0 (equivalently: line 1, col 1) means the very first byte of the input is already wrong. The parser never got to read a value — something failed before it could start.

The five most common root causes

1. Empty string

Passing an empty string or null to the parser fails immediately.

JSON.parse("")        // SyntaxError: Unexpected end of JSON input
JSON.parse(null)      // SyntaxError: Unexpected token n…

Fix: guard before calling parse.

const data = text?.trim() ? JSON.parse(text) : null;

2. Server returned HTML instead of JSON

An error page, login redirect, or misconfigured endpoint returns HTML starting with <!DOCTYPE or <html>. The '<' character at byte 0 is not valid JSON.

// ✗ fetch without checking status
const data = await fetch('/api/users').then(r => r.json());

// ✓ check ok first
const res = await fetch('/api/users');
if (!res.ok) throw new Error(`HTTP ${res.status}: ${await res.text()}`);
const data = await res.json();

3. UTF-8 BOM (bytes EF BB BF)

Some text editors and Windows tools prepend a byte-order mark. Most JSON parsers treat the BOM as invalid characters.

// Strip the BOM before parsing
const cleaned = text.replace(/^\uFEFF/, "");
const data = JSON.parse(cleaned);

4. The string "undefined"

In JavaScript, JSON.stringify(undefined) returns undefined (not the string "undefined"). If you then pass that to JSON.parse you get 'Unexpected token u at position 0'.

JSON.stringify(undefined)              // undefined (not a string!)
JSON.parse(JSON.stringify(undefined))  // TypeError or SyntaxError

// Fix: serialize null when the value is absent
JSON.stringify(value ?? null)

5. Mismatched Content-Type

If you call response.json() on a response whose body is plain text, XML, or an empty body, you get a position-0 error. Always verify the Content-Type header or log the raw body before parsing.

Debug steps

  • Log the raw response string and check its length and first character before calling parse.
  • Check text.charCodeAt(0) === 0xFEFF to detect a BOM.
  • Check text.trimStart().startsWith("<") to detect HTML.
  • Paste the raw bytes below to see exactly what character is at position 0.

Paste your JSON into the validator →

Get the exact line, column, and a fix hint in seconds — no upload, no signup.

Open JSON Validator →

Frequently asked questions

My response looks like JSON in browser DevTools but still fails at position 0 — why?+

The browser's Network panel may decode a BOM automatically. Log text.charCodeAt(0) before parsing — if it's 65279 (0xFEFF), strip it with text.replace(/^\uFEFF/, '').

How do I prevent the HTML-instead-of-JSON problem?+

Always check response.ok before calling .json(). Log the raw response body when you get unexpected status codes.

Can valid JSON start with a space?+

Yes — RFC 8259 allows optional leading whitespace (spaces, tabs, CR, LF). The problem is non-whitespace characters like '<' or a BOM.

Related tools & guides