Lingua-e
← Errors
JavaScript

SyntaxError: Unexpected end of JSON input: What It Means and How to Fix It

Error reference

SyntaxError: Unexpected end of JSON input

Language

JavaScript

Severity

Medium

When it happens

This SyntaxError is thrown by JSON.parse() when the string it receives is not valid JSON.

Potential fixes

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.

Related errors

Deep dive

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.

Potential fixes

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.

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

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);
  }
}

Practice in English

How would you explain a SyntaxError: Unexpected end of JSON input to a fellow dev? Choose the right phrase:

"I hit an error...

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