---
title: "SyntaxError: Unexpected end of JSON input"
description: "JSON.parse throws \"Unexpected end of JSON input\" when the text is empty or cut short. Learn why it happens with fetch and storage, and how to parse JSON safely."
url: https://www.danny.engineering/answer/javascript/syntaxerror-unexpected-end-of-json-input
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T18:58:00.000Z
tags: ["Javascript"]
---

# SyntaxError: Unexpected end of JSON input

## The short answer


`JSON.parse` reached the end of the string before it had a complete value. Most of the time the string is empty: an API returned no body, `localStorage` had nothing saved, or a file was empty. Check the raw text before you parse it, and handle the empty case.


```javascript
const response = await fetch('/api/user');
const text = await response.text();
const data = text ? JSON.parse(text) : null;
```


## Why it happens


JSON is strict. `JSON.parse` must see one complete value, such as an object, array, string, number, `true`, `false` or `null`. An empty string is not valid JSON, and neither is text that stops halfway through, such as `{"name": "Ada"` with no closing brace. When the parser runs out of characters, it throws this error.


The wording differs between engines. Chrome and Node say "Unexpected end of JSON input", Firefox says "JSON.parse: unexpected end of data", and Safari says "JSON Parse error: Unexpected EOF". They all mean the same thing.


Typical causes are:

- Calling `response.json()` on a response with no body, such as a `204 No Content` or a `DELETE` that returns nothing.
- Calling `response.json()` on an error response from a server or proxy that sent an empty body.
- Reading a missing key from storage. `localStorage.getItem` returns `null`, and `JSON.parse(null)` actually returns `null`, but an empty string saved earlier will throw.
- A response that was cut off by a timeout, a crashed server or a file written halfway.

Its close cousin, "Unexpected token < in JSON at position 0", means the body was there but was HTML, usually an error page or the wrong URL. Newer engines phrase it as "is not valid JSON".


## How to fix it


### Check the response before parsing


Look at the status and the body before you treat it as JSON. This also catches HTML error pages.


```javascript
async function getJson(url) {
  const response = await fetch(url);

  if (!response.ok) {
    throw new Error(`Request failed with ${response.status}`);
  }

  if (response.status === 204) return null;

  const text = await response.text();
  return text ? JSON.parse(text) : null;
}
```


Reading the body as text first means you can log it when parsing fails, which makes the real problem obvious.


### Wrap JSON.parse in try...catch


When the input comes from outside your code, such as storage, a file, a message or user input, expect that it might be invalid.


```javascript
function safeParse(text, fallback = null) {
  try {
    return JSON.parse(text);
  } catch (error) {
    console.warn('Invalid JSON:', text);
    return fallback;
  }
}

const settings = safeParse(localStorage.getItem('settings'), {});
```


### Fix the server so it sends valid JSON


If you own the API, make every JSON endpoint return a JSON body, including errors. Return `null`, `{}` or an error object rather than an empty body, and set the `Content-Type: application/json` header. Then clients do not need special cases for each endpoint.


### Check the content type when you are not sure


For endpoints that sometimes return JSON and sometimes do not, check the header first.


```javascript
const type = response.headers.get('content-type') ?? '';
const data = type.includes('application/json')
  ? await response.json()
  : await response.text();
```


## Common pitfalls

- Parsing something that is already an object. `JSON.parse` turns its argument into a string first, so an object becomes `"[object Object]"` and fails with a different error.
- Calling `JSON.parse(undefined)`. It becomes the string `"undefined"`, which is not valid JSON.
- Building JSON by hand with string concatenation. Use `JSON.stringify` so quotes and special characters are escaped.
- Reading a file in Node while another process is still writing it. Write to a temporary file and rename it, so readers never see half a file.
- Swallowing the error silently. Log the raw text in the `catch` block, or you will not know what the server actually sent.

## Related reading

- [MDN: JSON.parse()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse)
- [MDN: SyntaxError: JSON.parse: bad parsing](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/JSON_bad_parse)
