---
title: "\"Unexpected End of JSON Input\": What It Means and How to Fix It"
url: https://apyhub.com/blog/unexpected-end-of-json-input
author: ApyHub
published: 2026-11-17T10:28:05Z
---

# "Unexpected End of JSON Input": What It Means and How to Fix It

## Introduction

**"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.

## What 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.

## The common causes

1. **The API returned no body.** A `204 No Content`, a `DELETE` that returns nothing, or an endpoint that returns an empty `200`. Calling `response.json()` on it fails.
2. **The request failed and you parsed anyway.** A `401`, `404` or `500` can come back with an empty body. The code skips the status check and goes straight to `.json()`.
3. **The response was cut off.** A timeout, a dropped connection, or a proxy that closes early leaves half a JSON document.
4. **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.
5. **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?](https://apyhub.com/blog/what-is-cors-and-why-is-it-blocking-my-request)

## How to fix it

### Check the status before parsing

```js
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](https://voiden.md) or Postman, and look at three things:

* **Status code:** `204`, `4xx` or `5xx` explain most cases.
* **`Content-Type`:** if it isn't `application/json`, don't parse it as JSON.
* **`Content-Length` and the body:** `0`, or a body that stops mid-way, confirms an empty or cut-off response.

### Guard storage reads

```js
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.

## Validate JSON before you trust it

To check a payload in a pipeline or from an AI agent, the [JSON Formatter & Validator API](https://apyhub.com/freedailypro/service/code-data-formatting-api) returns whether the text is valid and why not. We sent it an empty string:

```json
{
  "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](https://apyhub.com/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 →**](https://apyhub.com/freedailypro/service/code-data-formatting-api)

## Conclusion

"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 →**](https://apyhub.com/)

## FAQ

### 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](https://voiden.md) or Postman, and check the status, `Content-Type` and body. To check the body itself, send it to the [JSON Formatter & Validator API](https://apyhub.com/freedailypro/service/code-data-formatting-api), or ask an AI assistant connected to [ApyHub MCP](https://apyhub.com/mcp) to validate it.

## About ApyHub

[ApyHub](https://apyhub.com/) 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](https://apyhub.com/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](https://apyhub.com/api-provider).

## Sources

* Error messages from tests run in Node.js 22 and Python 3.13, November 17, 2026
* ApyHub, [JSON Formatter & Validator API](https://apyhub.com/freedailypro/service/code-data-formatting-api): empty input tested November 17, 2026
