Quick answer

This error means JSON.parse() reached the end of the text while it still expected more. Two things cause it almost every time: the text is empty (an empty string, or a response with no body), or the JSON is truncated (a missing closing brace or bracket).

The same error in other places

Example that fails

JSON.parse("");          // empty text
JSON.parse('{"a": 1');   // missing closing brace

Cause 1: an empty response

The server replied with status 204 (No Content), an empty body, or crashed before sending data. Calling res.json() on that fails.

async function getJson(url) {
  const res = await fetch(url);
  if (!res.ok) throw new Error("HTTP " + res.status);
  const text = await res.text();
  if (!text.trim()) return null;   // nothing to parse
  return JSON.parse(text);
}

Cause 2: truncated JSON

The text was cut off. Typical reasons are a response size limit, a stream that closed early, a file that was not fully written, or a copy-and-paste that missed the last lines. Check that the number of { matches } and [ matches ]. Paste the text into the JSON Formatter to see where it stops, or try the JSON Repair tool, which can close missing brackets.

Cause 3: a missing value

const raw = localStorage.getItem("settings");
const settings = raw ? JSON.parse(raw) : {};   // safe

Note that JSON.parse(null) returns null without an error, but an empty string always throws.

The same problem in Python

import json

text = response.text
if not text.strip():
    data = None
else:
    data = json.loads(text)

Have broken JSON right now?

Paste it into the free JSON Repair tool. It runs in your browser, so nothing is uploaded.

Open JSON Repair tool

FAQ

Why does this happen only sometimes?

Usually the server sometimes returns an empty or partial body, for example during a timeout or an error. Log the raw text before parsing to see what arrived.

Does a 204 response cause this?

Yes. A 204 response has no body by design, so calling res.json() on it throws this error.

Can the file be too big?

Very large files can be cut by a proxy or a size limit. If the file is large, the JSON Splitter can break it into smaller parts.

Related guides