Nifty Dev Tools
  1. Home
  2. Guides
  3. Unexpected end of JSON input

Unexpected end of JSON input

The text ended before the JSON did: usually an empty response body, or one that was cut off. Firefox calls it “unexpected end of data”, Safari “Unexpected EOF”.

The exact message

A response cut off inside an array (the worked example below)
{
  "id": 7,
  "name": "Ada",
  "tags": ["math", "code",
Chrome, Edge, Node.js
SyntaxError: Unexpected end of JSON input
Firefox
SyntaxError: JSON.parse: unexpected end of data at line 4 column 27 of the JSON data
Safari
SyntaxError: JSON Parse error: Unexpected EOF
An empty response body, or JSON.parse("")
Chrome, Edge, Node.js
SyntaxError: Unexpected end of JSON input
Firefox
SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
Safari
SyntaxError: JSON Parse error: Unexpected EOF

With fetch: await response.json()

Chrome, Edge
SyntaxError: Failed to execute 'json' on 'Response': Unexpected end of JSON input
Node.js
SyntaxError: Unexpected end of JSON input
Firefox
SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
Safari
SyntaxError: The string did not match the expected pattern.
Cut off right after a value: Chrome and Firefox name what they expected instead
{"id": 7, "tags": ["api", "json"
Chrome, Edge, Node.js
SyntaxError: Expected ',' or ']' after array element in JSON at position 32 (line 1 column 33)
Firefox
SyntaxError: JSON.parse: end of data when ',' or ']' was expected at line 1 column 33 of the JSON data
Safari
SyntaxError: JSON Parse error: Expected ']'

Measured in Chrome 158, Firefox 159, Safari's engine (WebKit 27.2) and Node.js 22. Edge uses the same engine as Chrome.

What it means

The parser reached the end of the text while it was still waiting for something: the first value, a property name, or the value after a comma or a colon. Either there was no JSON at all, or the text stopped too early.

What causes it

  • An empty response body. By far the most common cause. response.json() on a 204 No Content response, on a HEAD request, on an error response without a body, or on an endpoint that answers with nothing when it has nothing to say. An empty string isn't JSON, so even JSON.parse("") fails.
  • An empty value. An empty file, an empty form field, or an environment variable that is set but empty, passed straight to JSON.parse.
  • A response that was cut off. A download interrupted by a timeout or a dropped connection, a proxy or logger that truncates long bodies, a stream that was parsed before it finished, or a file read while another program was still writing it.
  • Text that was cut when copying. A log line or terminal output that stops at a maximum length.

If the text is cut right after a complete value, Chrome and Firefox name what they expected instead (see the third sample above), but the cause and the fix are the same.

How to find the spot

The spot is always the end of the text, so Chrome and Node.js don't give a position. Firefox does: “line 4 column 27” in the example is the very last character.

First check whether there is any text at all. Log it before parsing it: console.log(JSON.stringify(text)) shows an empty body as "". In the browser, open the developer tools, select the request in the Network tab and look at Response and the status code.

If there is text, paste it into the JSON Formatter. It shows the line and column where the text ends and which bracket is still open, so you can see how much is missing.

How to fix it

If the body can be empty on purpose (a 204, a DELETE endpoint), read it as text first and parse it only when there is something to parse:

const res = await fetch(url);
const text = await res.text();
if (!res.ok) throw new Error(`HTTP ${res.status}: ${text.slice(0, 200)}`);
const data = text.trim() ? JSON.parse(text) : null;

This also turns an error response into a clear message instead of a parse error.

If the JSON was cut off, repairing the text isn't enough: the missing part is gone. Load it again, raise the timeout or size limit that cut it, or wait until the file is completely written. On your own server, send a body whenever the response says Content-Type: application/json ({}, [] or null), or answer 204 No Content and don't parse it on the client.

Worked example

This response was cut off in the middle of the tags array:

The input

{
  "id": 7,
  "name": "Ada",
  "tags": ["math", "code",
Chrome and Node.js say
SyntaxError: Unexpected end of JSON input
The JSON Formatter says
Hmm, something's off on line 4, column 27: the JSON ends too early. A closing ] is missing 🤔

Press “Fix all 2” and the JSON Formatter makes these changes:

  • Added the missing ]} at the end (line 4)
  • Removed a trailing comma (line 4)

The result:

{
  "id": 7,
  "name": "Ada",
  "tags": ["math", "code"]}

Open this example in the JSON Formatter

Fix it closes the open brackets, so you can read and format what arrived. Whatever came after "code", is still missing, though: if it matters, load the data again. When the text stops inside a string or right after a colon, Fix it doesn't guess the rest; the message still shows you where it ends.