fjordtools / Errors / Unexpected end of JSON input

Unexpected end of JSON input

The parser reached the end of the text while it still expected more. Either the text is empty, or it was cut off where a value should come next, for example right after a colon or a comma. Check whether the response body is empty or truncated before you parse it.

Fjordtools team · Seen in Chrome and Node (V8) · Published · 1 min read

How browsers word it

Chrome and Node (V8)Unexpected end of JSON input
FirefoxJSON.parse: unexpected end of data at line 1 column 1 of the JSON data
SafariJSON Parse error: Unexpected EOF

Common causes

  1. The body is empty

    A 204 No Content response, a failed request or a missing file gives an empty string, and parsing an empty string always fails.

    Input

    (empty)

    JSON Formatter says

    Nothing to parse. The input is empty
  2. The text is only whitespace

    A body that holds just a newline or spaces fails the same way.

    Input

     

    JSON Formatter says

    Nothing to parse. The input is empty
  3. The text was cut off where a value should be

    A dropped connection, a size limit or a file that was not saved completely can stop the text right after a colon or a comma.

    Input

    {"a":

    JSON Formatter says

    Unexpected end of input. A value is missing
  4. The text was cut off inside a string

    If the cut falls inside a string, current engines report an unterminated string rather than this message.

    Input

    {"a": "hel

    JSON Formatter says

    Unterminated string. A closing double quote is missing

How to fix it

  1. Log the raw response text and its length before parsing.
  2. Check the status code. A 204 has no body, so skip .json() for it.
  3. Compare Content-Length with what you received, and look for proxies or functions that limit the response size.
  4. If you read a file, make sure it was written completely before it is parsed.

Related errors

More error explainers are coming

See all errors.