"Unexpected end of JSON input" in JavaScript — What It Means and How to Fix It

What the JavaScript error "Unexpected end of JSON input" means and how to fix it.

Published October 30, 2026

JSON.parse() received an empty or truncated string, so it ended before a complete value was read.

What it means and why it happens

Typical causes: empty responses, cut-off network data or parsing localStorage values that do not exist.

Read the full message: it names the file and line where the problem was detected, which is the best place to start.

What you see

run the code
SyntaxError: Unexpected end of JSON input

Practical examples

Code that triggers it

JSON.parse('')

The fix

const text = await res.text()
const data = text ? JSON.parse(text) : null

Safe parse

function safeParse(s, fallback = null) { try { return JSON.parse(s) } catch { return fallback } }

Common mistakes

  • Parsing empty bodies (204 responses).
  • Parsing undefined/null.
  • Truncated responses from proxies.

Key takeaways

  • SyntaxError: Unexpected end of JSON input: JSON.parse() received an empty or truncated string, so it ended before a complete value was read.
  • Read the message and the line number first.
  • Fix the cause, don't just silence the error.

FAQ

response.json() fails?

The body is empty or not valid JSON; check status and text first.

How do I find which line caused SyntaxError: Unexpected end of JSON input?

The error message and stack trace show the file and line; start there and work backwards.

More JavaScript articles