SyntaxError: Unexpected end of input (JavaScript) — how to fix
Uncaught SyntaxError: Unexpected end of input
SyntaxError: Unexpected end of JSON input
SyntaxError: Failed to execute 'json' on 'Response': Unexpected end of JSON input
Quick fix: For code: find the unclosed bracket, quote or backtick (format the file with Prettier or check matching braces in your editor). For Unexpected end of JSON input: the string you passed to JSON.parse() / res.json() is empty — check res.ok and await res.text() first, and don't call .json() on 204 or no-cors responses.
What it means
The JavaScript (or JSON) parser ran out of characters before the structure was complete. There are two very different situations with the same message:
Unexpected end of input— your source code has something unclosed.Unexpected end of JSON input—JSON.parse()orresponse.json()got an empty or cut-off string.
Cause 1: unclosed brace, parenthesis or bracket
function greet(name) {
if (name) {
console.log(`Hi ${name}`);
}
// missing } for the function
The error points at the last line of the file, not where the brace is missing. To find it:
- Run Prettier (
npx prettier --check file.js) or ESLint — they report the location more precisely. - Use your editor's bracket matching / fold markers and look for the block that "swallows" the rest of the file.
- Check callbacks:
}, []);and});are easy to get wrong inuseEffect,addEventListener, and promise chains.
Cause 2: unclosed string or template literal
A missing closing backtick makes the rest of the file one string:
const html = `<div>${title}</div>;
Syntax highlighting usually makes this obvious — everything after it is coloured as a string.
Cause 3: inline code in HTML attributes
onclick="doThing({a: 1)" or code generated into a <script> tag can be truncated. Check what the browser actually received (DevTools → Sources), not your template.
Cause 4: JSON.parse("") — empty input
JSON.parse(""); // SyntaxError: Unexpected end of JSON input
JSON.parse(localStorage.getItem("cart")); // fine for null, but "" fails
Guard against empty strings:
const raw = localStorage.getItem("cart");
const cart = raw ? JSON.parse(raw) : [];
Cause 5: response.json() on an empty body
fetch responses with no body — 204 No Content, many DELETE/POST endpoints, errors from the server, or any mode: "no-cors" response (opaque, always empty) — throw when you call .json().
const res = await fetch("/api/items", { method: "POST", body });
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const text = await res.text();
const data = text ? JSON.parse(text) : null;
Don't use mode: "no-cors" to "fix" CORS; you can't read that response at all. Fix CORS on the server instead.
On the server side (Express etc.), make sure the handler actually sends JSON: res.json(obj), not res.end() or res.sendStatus(200).
Cause 6: a truncated file or download
A partially written JSON file, a timeout mid-response, or a bundler that crashed can leave incomplete content. Re-download / rebuild and check the file's end with tail -c 200 file.json.
Last updated 2026-11-20.