01Introduction
"Unexpected end of JSON input" means the parser reached the end of the text before the JSON was complete. Most of the time the text was empty: an API returned no body, and your code called JSON.parse() or response.json() on it anyway. Less often, the JSON was cut off partway through.
The fix is almost never in the JSON itself. It's in checking what you received before you parse it.
02What triggers it, tested
We ran common inputs through JSON.parse in Node 22:
| Input | Error |
|---|---|
| "" (empty string) | Unexpected end of JSON input |
| " " (only spaces) | Unexpected end of JSON input |
| {"a": (stops after a colon) | Unexpected end of JSON input |
| {"name": "Ad (stops inside a string) | Unterminated string in JSON at position 12 |
| [1, 2 (missing closing bracket) | Expected ',' or ']' after array element in JSON at position 5 |
And through response.json(), which parses a fetch response body:
| Response | Error |
|---|---|
| 204 No Content | SyntaxError: Unexpected end of JSON input |
| 200 with an empty body | SyntaxError: Unexpected end of JSON input |
| 500 with an HTML error page | SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON |
So "unexpected end" almost always means empty. An HTML error page gives a different error, Unexpected token '<', which is worth knowing because the two are often confused.
In Python, the same empty input raises JSONDecodeError: Expecting value: line 1 column 1 (char 0), which is Python's version of this error.
03The common causes
- The API returned no body. A
204 No Content, aDELETEthat returns nothing, or an endpoint that returns an empty200. Callingresponse.json()on it fails. - The request failed and you parsed anyway. A
401,404or500can come back with an empty body. The code skips the status check and goes straight to.json(). - The response was cut off. A timeout, a dropped connection, or a proxy that closes early leaves half a JSON document.
- Reading from storage that's empty.
JSON.parse(localStorage.getItem("cart"))when nothing was saved, or reading a file that hasn't been written yet. - A CORS or network failure in the browser. The request never got a readable response. See What is CORS and why is it blocking my request?
04How to fix it
Check the status before parsing
const res = await fetch(url);
if (!res.ok) {
throw new Error(`Request failed: ${res.status}`);
}
if (res.status === 204) {
return null; // no body to parse
}
const text = await res.text();
const data = text ? JSON.parse(text) : null;Reading the body as text first means an empty body becomes null instead of an exception, and you can log the raw text when parsing fails.
Look at the raw response
Before changing any code, see what the server really sent. Run the request in curl with -i to show the status and headers, or in Voiden or Postman, and look at three things:
- Status code:
204,4xxor5xxexplain most cases. Content-Type: if it isn'tapplication/json, don't parse it as JSON.Content-Lengthand the body:0, or a body that stops mid-way, confirms an empty or cut-off response.
Guard storage reads
const saved = localStorage.getItem("cart");
const cart = saved ? JSON.parse(saved) : [];Fix cut-off responses at the source
If the body is truncated, the cause is the connection, not your parser: raise the timeout, check proxy and load balancer limits, and retry idempotent requests.
05Validate JSON before you trust it
To check a payload in a pipeline or from an AI agent, the JSON Formatter & Validator API returns whether the text is valid and why not. We sent it an empty string:
{
"valid": false,
"error": "Unexpected end of JSON input"
}When the JSON is valid, it returns the formatted JSON plus key and depth counts. The endpoint is also available through ApyHub MCP, so Claude, ChatGPT, Cursor and other AI assistants can validate a payload with a tested endpoint instead of guessing, which gives consistent results and uses fewer tokens.
Validate JSON with the JSON Formatter & Validator API →
06Conclusion
"Unexpected end of JSON input" means there was nothing, or not enough, to parse. Check the status code before parsing, return null for 204 and empty bodies, read the raw response when it fails, and guard reads from storage. If responses are being cut off, fix the timeout or the connection.
Start free on ApyHub, no card required →
07FAQ
What does "unexpected end of JSON input" mean?
The parser hit the end of the text before the JSON was complete. Usually the text was empty.
How do I fix "unexpected end of JSON input"?
Check the response status before parsing, skip parsing for 204 and empty bodies, and read the body as text first so you can see what arrived.
Why does response.json() throw this error?
The response had no body, often a 204 No Content or a failed request. response.json() can't parse an empty body.
Is "Unexpected token '<'" the same error?
No. That one means the response was HTML, usually an error page, not JSON.
What is the Python version of this error?
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) for an empty string.
How can I test the API response?
Run the request with curl -i, or in Voiden or Postman, and check the status, Content-Type and body. To check the body itself, send it to the JSON Formatter & Validator API, or ask an AI assistant connected to ApyHub MCP to validate it.
08About ApyHub
ApyHub runs a catalog of ready-made APIs and is the trusted operational layer between your application and the services it depends on, so your team builds and maintains less of its own code and ships faster. One subscription, billed in atoms, covers the whole catalog of over 1,500 endpoints and capabilities, and it keeps growing. Every service is verified before it is listed and carries machine-readable certification covering GDPR, SOC 2 and ISO 27001. Every endpoint is MCP-ready by default. ApyHub runs dedicated EU and US regions and serves more than 65,000 developer workspaces every month. Start on the free tier with no card, or publish your own API on ApyHub.
09Sources
- Error messages from tests run in Node.js 22 and Python 3.13, November 17, 2026
- ApyHub, JSON Formatter & Validator API: empty input tested November 17, 2026
