JSON Errors / JavaScript
Unexpected end of JSON inputHow to Fix: Unexpected end of JSON input
JSON.parse() reached the end of the string while the document was still incomplete - an object or array was opened but never closed, or the string was empty to begin with.
The two dominant causes are parsing an empty string (very common with empty HTTP response bodies and missing localStorage keys) and data that was truncated somewhere between producer and consumer.
Common causes
1. Parsing an empty string
JSON.parse("") throws exactly this error. Typical sources: a 204 No Content response passed to response.json(), or localStorage.getItem() returning null/"" for a key that was never set.
JSON.parse("") // Unexpected end of JSON input
JSON.parse(localStorage.getItem("missing")) // same2. Truncated response or file
A connection dropped mid-transfer, a log line hit a size limit, or a file write was interrupted - the JSON simply stops: {"users": [{"name": "Ada"
{"users": [{"name": "Ada"3. Reading a stream before it finishes
Assembling chunks from a stream and parsing before the final chunk arrives produces valid-looking but incomplete JSON.
How to fix it
const raw = localStorage.getItem("settings");
const settings = raw ? JSON.parse(raw) : defaultSettings;
// For fetch: check status before parsing
const res = await fetch("/api/data");
const data = res.status === 204 ? null : await res.json();const raw = await res.text();
console.log("length:", raw.length);
console.log("tail:", raw.slice(-100)); // does it end with } or ]?Frequently asked questions
Why does an empty string throw this instead of returning null?
An empty string is not a JSON document - the JSON grammar requires at least one value. The parser reads to the end looking for that value, finds nothing, and reports that the input ended unexpectedly.
How is this different from "Unexpected token" errors?
"Unexpected token" means the parser found a wrong character at a specific position. "Unexpected end of input" means every character it saw was fine - the document just stopped before it was complete. Wrong character vs. missing characters.
My JSON file looks complete - why does Node still throw this?
Check for an empty file being read instead of the one you expect (wrong path resolves to an empty file), a race where you read while another process writes, or a BOM/encoding issue making the content unreadable. Log the raw string length first.
Last updated:
Related JSON errors
You might also need
Free and in-browser, like everything on JSON Console. Browse all tools →