“Unexpected end of JSON input”: What It Means and How to Fix It
This error means exactly one thing: the parser ran out of characters before the document finished. The string you parsed is incomplete — empty, cut off, or missing a closing bracket. The fix depends on which.
In short
'Unexpected end of JSON input' means the parser ran out of characters before the document finished — the string was incomplete: an empty response, a payload truncated by a size limit or dropped connection, or a document you hand-edited that's missing a closing brace or bracket. Check the raw text first, then validate: an error position at the very end confirms truncation.
Cause 1: you parsed an empty string
The most common case in JavaScript, and it usually comes from fetch: calling response.json() on a 204 No Content, an error page, or an empty body throws this exact error — because JSON.parse("") fails with 'unexpected end of input' (nothing was there to parse). The same happens with localStorage.getItem returning null coerced to a string.
// Debug pattern: look at the body BEFORE parsing
const res = await fetch(url);
const text = await res.text(); // read raw first
console.log(res.status, JSON.stringify(text.slice(0, 200)));
const data = text ? JSON.parse(text) : null; // guard the empty caseCause 2: the payload was truncated
Logs cap line length, clipboards cut huge selections, proxies and serverless platforms enforce body limits, and connections drop mid-response. The document starts perfectly and just… stops — often mid-string, thousands of lines in, where you'd never spot it by eye. The tell: paste it into a validator and the reported error position is at (or near) the very end of the input.
Cause 3: an unclosed bracket you wrote
Hand-edited JSON with a missing } or ] parses fine all the way down, then hits end-of-input still expecting closure. Finding WHICH bracket is unclosed in a nested document is the annoying part — and exactly what bracket-matching editors solve: open the document in our validator, click next to each opening brace, and the editor highlights its partner (or conspicuously fails to).
The systematic fix
First, confirm what you actually have: log or paste the raw text — is it empty? Does it end mid-value? Second, validate: the error position at the absolute end means truncation or unclosed structure; earlier means a different syntax error wearing a similar mask. Third, repair appropriately: re-fetch truncated payloads from the source (the missing bytes aren't recoverable from the fragment), guard empty-body cases in code, and for unclosed brackets let one-click Repair complete the structure — then verify the result is the document you intended.
| Cause | Tell-tale sign | Fix |
|---|---|---|
| Empty string parsed | res.json() on a 204 or empty body | Guard: only parse when the text is non-empty |
| Truncated payload | Error position lands at the very end of the input | Re-fetch from the source — missing bytes can't be recovered |
| Unclosed bracket | Parses fine, then hits end-of-input | Use bracket matching, or one-click Repair to close it |
Sources
Frequently asked questions
Why does fetch().json() throw this on a successful (200) response?
Because success doesn't guarantee a body. Some endpoints return 200 with an empty body, or HTML from an error/redirect page. Read response.text() first while debugging — the raw body tells you instantly whether you have a JSON problem or an API-contract problem.
Can automatic repair recover a truncated document?
It can close the open structures so the fragment parses — useful for inspecting what survived — but the cut-off data is simply gone. For analysis, repair away; for production data, re-fetch from the source.
Is 'Unexpected end of input' (without 'JSON') the same error?
Same family, different parser: JavaScript's own syntax parser says 'Unexpected end of input' for unclosed JS code, while JSON.parse says 'Unexpected end of JSON input'. Both mean premature end; the JSON variant points you at the string you parsed, not your script.
Paste the failing payload into the JSON Validator — if the error position is at the end, you've confirmed truncation in five seconds.
Diagnose it nowKeep working
Related tools
JSON Validator
Strict RFC 8259 validation with the exact line and column of every error — plus one-click repair for broken JSON.
JSON Parser
Run a real JSON parse in your browser — see exactly what your application will see, or exactly where it will fail.
JSON Formatter
Format, beautify, and validate JSON instantly in a professional VS Code-style editor — free, fast, and 100% private.
String to JSON
Paste an escaped JSON string — with all its backslashes — and get the real, formatted JSON back.
JSON Repair
Paste JSON that almost parses — single quotes, trailing commas, comments, a truncated tail — and get valid, formatted JSON back.