Skip to content

JavaScript · browsers and Node.js

Unexpected end of JSON input

The parser reached the end of the text while it still expected more: a value, a closing quote, or a closing bracket.

Updated · 2 min read

console
Chrome, Edge, Node.jsSyntaxError: Unexpected end of JSON input
fetch() in ChromeSyntaxError: Failed to execute 'json' on 'Response': Unexpected end of JSON input
FirefoxSyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
SafariSyntaxError: JSON Parse error: Unexpected EOF

Find it in your JSON

Paste the text that failed. The checker points at the exact spot, and Repair fixes most syntax mistakes.

Input · JSON

Invalid JSON

Line 5, column 33

Unexpected end of input: 2 brackets never closed.

3  "items": [4    { "sku": "KB-01", "qty": 1 },5    { "sku": "MS-11", "qty": 2 }                                 ^

Fix: Add the missing ']'.

Also see line 3, column 12: '[' opened here.

Why this happens
Invalid JSONRuns in your browser

Load from a URL

Your browser fetches it directly, so the server must allow cross-origin requests (CORS).

Why it happens

Three causes cover almost every case.

1. The body is empty. JSON.parse('') throws this exact error. You get an empty string from a 204 No Content response, a HEAD request, a server error with no body, or a file that exists but has nothing in it. In Firefox the position gives it away: line 1, column 1.

2. The JSON was cut off. The text starts correctly and stops partway, like the sample in the checker above. Typical sources:

  • a network response that closed early, or a proxy with a body size limit
  • a log line or database column shorter than the value (a VARCHAR(255) holding a 300-character document)
  • an LLM reply that hit its token limit mid-object
  • a copy and paste that missed the last line

3. A bracket is missing. Hand-edited JSON with an unclosed { or [ runs out of text before the parser finds the closer. The checker names the bracket and the line it opened on.

How to fix it

fetch

Check the status and read the body as text before parsing, so an empty body becomes a clear decision instead of an exception:

const res = await fetch('/api/orders/1042');
if (!res.ok) throw new Error(`Request failed: ${res.status}`);

const text = await res.text();
const order = text ? JSON.parse(text) : null; // 204 or empty body

res.json() is fine when the endpoint always returns a body. Use the pattern above for endpoints that can answer 204.

axios

axios parses JSON for you. An empty body arrives as response.data === '' instead of throwing, so the error usually shows up later when you call JSON.parse(response.data) yourself. Drop the manual parse, or guard it with the same empty check.

Node.js files

JSON.parse(fs.readFileSync('config.json', 'utf8')) throws this when the file is empty or another process is still writing it. Write to a temporary file and rename it when done, so readers never see a half-written file.

Truncated text from a model or a stream

Ask for less output, raise the token limit, or stream and parse only after the final chunk arrives. To salvage what you have, paste it into JSON Repair, which closes the open structures. Check the result: whatever was cut off is still gone.

A missing bracket

Paste the JSON into the checker above or the JSON validator. It reports Unexpected end of input: 1 bracket never closed and highlights where that bracket opened, which is faster than counting braces by eye.

Same error in other languages

  • Python: an empty string raises json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0). See JSONDecodeError: Expecting value.
  • Go: json.Unmarshal on an empty or truncated []byte returns unexpected end of JSON input, word for word.
  • Unclosed string: text cut off inside a string gives Unterminated string in JSON in Chrome instead.

For the other errors JSON parsers throw, see common JSON errors.

Questions

Why do I get this error when the API call succeeded?

A 200 or 204 status does not guarantee a body. Read the response with res.text() first and check it is not empty before parsing.

Can Repair fix truncated JSON?

It closes the open strings, arrays and objects so the text parses again. The data that was cut off is still missing, so compare the result with the source before you use it.

Is this the same as "Unexpected token" errors?

No. "Unexpected token" means the parser found a character it did not expect. "Unexpected end" means it found nothing at all where it needed more.

Related errors

All common JSON errors

Esc