JSON.parse() is the standard way to turn a JSON string into a JavaScript object, and one of its most common failures is:
SyntaxError: Unexpected end of JSON input
It shows up in the browser, in Node.js, and in build tools, and it almost always means the same thing: the string you asked it to parse is empty, or it was cut off before reaching a complete JSON value. This guide covers every common cause and how to fix it.
JSON.parse() reads a string character by character and builds up a value. When it reaches the end of the string while still expecting more content — for example, it read an opening { but never found the matching } — it throws Unexpected end of JSON input.
In other words, the JSON is real but incomplete, which is different from parsing a non-JSON string (like HTML).
JSON.parse(''); // ❌ SyntaxError: Unexpected end of JSON input
An empty string contains no JSON value at all. This usually happens when a variable is unexpectedly '', null, or undefined.
const raw = localStorage.getItem('config'); // could be null
JSON.parse(raw); // ❌ throws if raw is null or ''
If a download or response is interrupted, you end up with half a JSON document:
// Intended: {"name":"Tom","age":30}
const broken = '{"name":"Tom","age":30'; // ❌ missing closing }
JSON.parse(broken);
const res = await fetch('/api/user');
const text = await res.text(); // partial if the request failed
JSON.parse(text); // ❌ throws when text is incomplete or HTML
Slicing, trimming, or concatenating strings can accidentally remove the last characters:
const full = '{"status":"ok"}';
const cut = full.slice(0, -1); // {"status":"ok"} -> {"status":"ok"
JSON.parse(cut); // ❌
Guard against empty input first:
function safeParse(str) {
if (!str || str.trim() === '') return null;
return JSON.parse(str);
}
Wrap parsing in try/catch so a bad payload can’t crash your app:
try {
const data = JSON.parse(raw);
} catch (e) {
console.error('Invalid JSON:', e.message);
}
Check the source is complete — verify the API returned a 200 status, the file finished downloading, or the response body is fully read before parsing.
Inspect the end of the string to confirm it ends with a closing } or ]:
console.log(raw.slice(-20)); // see the last 20 characters
Instead of staring at a long string, paste it into the JSON formatter. It parses the input immediately and reports the exact position where the JSON becomes invalid, so you can see precisely which character is missing. For the related error caused by HTML in your response, see Fix “Unexpected token < in JSON at position 0”.