Unexpected end of JSON input

Your JSON stopped early. The parser was still waiting for a closing brace, a closing quote, or in some cases for any content at all. This error gives you no position to look at, because the problem is what is missing rather than what is there. Nine times out of ten the string was simply empty.

How the message appears

The same fault, worded by different engines. V8 (Chrome, Node, Edge) changed its format around Chrome 109, so older and newer runtimes disagree about the same document.

  • SyntaxError: Unexpected end of JSON input
  • SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
  • SyntaxError: JSON Parse error: Unexpected EOF

The fix

Close everything you opened. In the example below both the inner object and the outer object are missing their closing brace.

Fails to parse
{
  "user": {
    "name": "Ada",
    "id": 7
Parses
{
  "user": {
    "name": "Ada",
    "id": 7
  }
}

The example travels in the URL fragment, which browsers never send to a server.

What causes it

The string is empty

JSON.parse("") throws exactly this. An empty string is not valid JSON. If you want an empty value, the JSON for that is null. This is by far the most common cause, and it usually means a response came back with no body.

The response had no body to parse

Either a 204 No Content reply, or a fetch body you already read. A Response body is a stream, and a stream can only be read once. Log it with res.text() and the later res.json() finds nothing left.

Truncated output

The file got cut short. A full disk, a killed process, a stream nobody flushed or closed, or a log line clipped at a length limit. Jump to the end of the file. If the closing brace is not there, whatever wrote it never finished.

An unclosed bracket, brace or string

The same thing from the other end. The parser is still inside a value when the input runs out. A missing closing quote is the sneakiest version, because everything after the opening quote gets swallowed as part of the string.

Worth knowing

  • Check for the empty case instead of catching the error. const data = text ? JSON.parse(text) : null is all it takes.
  • Check response.ok and the content-type header before you parse. A failed request usually returns HTML or nothing, and neither one is JSON.

Written by Vishnu Shankar.

Your JSON and text are processed entirely in your browser and are never sent anywhere. To understand how the site is used we record each page view (the page, the referrer and an approximate location from your IP address) and keep it for 90 days. Nothing is stored on your device unless you choose it. Accept to also store a device identifier here and record your browser and device characteristics, which is what lets a return visit be told from a new one. Decline and nothing is sent at all. We respect Do Not Track. See our privacy policy.