Lingua-e
JavaScript

What is a SyntaxError: Unexpected end of JSON input?

← Errors

This SyntaxError is thrown by JSON.parse() when the string it receives is not valid JSON. 'Unexpected end of input' specifically means the string was cut off before the JSON structure was complete, or the string was empty.

Why it happens

  • Parsing an empty string, for example from a failed fetch that returned no body.
  • A network response was truncated before it was fully received.
  • A file or database field that stores JSON was accidentally saved as empty or partially written.
  • String concatenation or interpolation produced a malformed JSON string.

Examples

JavaScript

Code that triggers the error

const raw = "";   // empty string from a failed fetch
const data = JSON.parse(raw);
console.log(data);

Error output

SyntaxError: Unexpected end of JSON input

How to fix it

Validate the string before parsing: check that it is non-empty and wrap JSON.parse() in a try/catch. When fetching JSON from an API, use response.json() instead of manually parsing response.text(), as it handles the Content-Type and empty-body cases more gracefully.

Fixed code

const raw = "";

try {
  const data = JSON.parse(raw);
  console.log(data);
} catch (err) {
  if (err instanceof SyntaxError) {
    console.error("Invalid JSON received:", raw);
  }
}

Related errors

Ready to practice your English at work?

Lingua-e has interactive exercises built around real developer conversations: standups, code reviews, retrospectives, and more. Practice until it comes naturally.

Try Lingua-e for free