Unexpected end of JSON input

ErrorJavaScript

SyntaxError: Unexpected end of JSON input

Where it stops

  "user": {
    "name": "Ada",
    "id":

What it means

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. Most often the string was empty.

Last verified on Chromium 145, Firefox 146 and WebKit 26.

It opens with the error marked. Nothing is uploaded. Find where the error is.

Usually
The string is emptyThe response had no body to parseTruncated outputAn unclosed bracket, brace or string
Fix
Finish the document.

The fix

Finish the document. The example below stops after "id":, so the value and both closing braces are missing. Give the key a value and close everything you opened.

broken.jsonFails to parse
{
  "user": {
    "name": "Ada",
    "id":
JSON.parse reaches the end of the text before the document is complete.

Lines 4, 5 and 6 change.

fixed.jsonParses
{
  "user": {
    "name": "Ada",
    "id": 7
  }
}
JSON.parse reads it.

How the message appears

Chrome and Node first, then Firefox, then Safari. The wording depends on where the text stops. The fourth line is what Firefox prints for an empty string. A document that stops right after a value gets a different message, and the last three lines show it.

Console
  • SyntaxError: Unexpected end of JSON inputChrome and Node
  • SyntaxError: JSON.parse: unexpected end of data at line 4 column 10 of the JSON dataFirefox
  • SyntaxError: JSON Parse error: Unexpected EOFSafari
  • SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
  • SyntaxError: Expected ',' or '}' after property value in JSON at position 44 (line 4 column 12)
  • SyntaxError: JSON.parse: end of data after property value in object at line 4 column 12 of the JSON data
  • SyntaxError: JSON Parse error: Expected '}'

What causes it

  1. 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.

  2. 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.

  3. 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.

  4. 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

NoteCheck for the empty case instead of catching the error. const data = text ? JSON.parse(text) : null is all it takes.

NoteCheck response.ok and the content-type header before you parse. A failed request usually returns HTML or nothing, and neither one is JSON.

What you put into the tools never leaves your browser. We count page views and keep them for 90 days. Accept and we also remember this browser, so a return visit is not counted as new. Privacy policy