Fix “Unexpected end of JSON input”
This error means the parser reached the end of the string while it was still waiting for a value, quote, bracket, or brace. The real fix is to identify why the input ended early.
Start with the actual response
Log or inspect the raw response before calling JSON.parse(). Do not only inspect the object you expected to receive. An empty body, an HTML error page, and a JSON document cut off halfway can all look like “the API returned bad JSON,” but each requires a different fix.
const text = await response.text();
console.log(response.status, response.headers.get("content-type"), text.length);
const data = JSON.parse(text);
If text.length is zero, first check whether the endpoint is allowed to return no content. A 204 No Content response should not be parsed as JSON. If the endpoint promises JSON, an empty body is a server or proxy contract violation.
Common syntax causes
A missing closing brace or bracket is the simplest cause:
{"user":{"id":42,"roles":["admin","editor"]}
The outer object never closes. An unfinished string such as {"name":"Ada} causes the same end-of-input condition because the parser is still waiting for a quote. Paste the exact text into the JSON Validator to get a line and column near the incomplete structure.
Truncated network and file input
When previously valid JSON fails only for large responses, investigate response-size limits, timeouts, interrupted streams, reverse-proxy buffering, and partial file writes. Compare the received byte count with the server’s log or expected file size. Retrying may hide the symptom temporarily; it does not explain why a response was truncated.
For files, write to a temporary path and rename only after the write succeeds. For HTTP, avoid parsing until the complete body is available unless you are using a streaming format designed for incremental processing, such as JSON Lines.
Do not silently replace bad data
A catch block that returns {} prevents the immediate exception but often creates a later bug: required fields disappear, authorization checks receive defaults, or the UI renders misleading empty state. Report the status, content type, body length, and a safe excerpt. Never log secrets or entire private payloads just to diagnose a parse failure.
Repair versus recovery
JSON Repair can close some obvious structures and fix syntax differences such as trailing commas. It cannot recover a missing field value or the second half of a network response. Use repaired output only after confirming that no meaningful data was lost.
Frequently Asked Questions
Does Unexpected end always mean a missing bracket?
No. Empty bodies and truncated network responses produce the same family of error.
Should I catch the error and return an empty object?
Usually no. Hiding malformed or missing data can move the failure to a less obvious place.
Can a JSON repair tool recover truncated content?
It can close obvious structures, but it cannot reconstruct values that were never received.