The Unexpected end of JSON input error always means one thing: your JSON is incomplete. The parser reached the end of the input while still waiting for a closing character. Here is what causes it and how to fix each case.

What the error means

A valid JSON document must be complete and self-contained. Every brace needs a match, every bracket needs a closing partner, and every string needs a closing quote. When JSON.parse() runs out of input before finding the closing character it expects, it throws this error.

JSON.parse('{"name": "John"');  // Unexpected end of JSON input
JSON.parse('');                 // Unexpected end of JSON input
JSON.parse('[1, 2, 3');         // Unexpected end of JSON input

Cause 1: Parsing an empty string

The most common trigger. You call JSON.parse() on a variable that turned out to be empty — often an API response with no body.

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

Cause 2: Truncated API response

Network timeouts, proxy buffer limits, and body size restrictions can cut a response off mid-stream.

Fix: Compare the Content-Length header with the bytes you received. If they differ, the response was truncated — raise your proxy limits or add a retry.

Cause 3: Missing closing brace or bracket

In hand-written JSON, an unclosed object or array is easy to miss when nesting is deep.

{
  "roles": ["admin", "editor"
}   // missing ] for the array

Fix: Count opening and closing brackets. An auto-indenting formatter makes mismatches obvious.

Cause 4: Unterminated string

A string missing its closing quote makes the parser read everything after it as part of the string until it hits the end of input. The reported position is at the very end, far from the real problem.

{"name": "Alice, "age": 30}
//          ^ missing closing quote

Validate & repair your JSON — Formatter

Paste incomplete JSON and the formatter shows you exactly where the structure breaks. Free and private.

Open tool →

Cause 5: Double-parsing

Calling JSON.parse() on something that is already an object coerces it to "[object Object]", which then fails. Only parse strings — check typeof data === 'string' first.

The error in Python

Python's equivalent is JSONDecodeError: Expecting value: line 1 column 1 (char 0), usually from passing an empty string or None to json.loads().

Defensive parsing pattern

function safeParse(text) {
  if (!text || !text.trim()) return null;
  try { return JSON.parse(text); }
  catch (e) { return null; }
}