Need help with your JSON?
Try our JSON Formatter tool to automatically identify and fix syntax errors in your JSON. JSON Formatter tool
Unexpected End of JSON Input: Causes and Fixes
The error Unexpected end of JSON input means the parser reached the end of a string or response body before it found one complete JSON value. Most of the time, that means you tried to parse an empty response, the payload was cut off before it finished downloading, or the JSON is missing a closing quote, bracket, or brace.
Quick answer
- Log the raw input and its length before parsing so you can see whether it is empty or cut off.
- If you use
fetch(), do not blindly callresponse.json()for204,304, orHEADresponses. - If the payload ends mid-object or mid-array, the parser is not the real problem. Something upstream is returning incomplete JSON.
What the error actually means
Valid JSON must contain one complete value such as an object, an array, a string, a number, true, false, or null. When the parser reaches the end too early, JavaScript engines commonly surface that as Unexpected end of JSON input. In other runtimes the wording may vary slightly, but the root issue is the same: the input ended before the JSON was complete.
The most common causes
- Empty input. Parsing
"", whitespace, or an HTTP response with no body will fail because there is no JSON value to parse. - Incomplete JSON text. A missing
},], comma, or closing quote leaves the structure unfinished. - Truncated API payloads. The server may start returning JSON and then stop because of a crash, timeout, proxy limit, failed compression, or interrupted connection.
- Parsing the wrong kind of response. An endpoint may return no content for some cases, or it may occasionally return HTML or plain text instead of JSON.
- Building JSON manually. String concatenation is a common source of half-finished objects and arrays.
Common examples
1. Parsing an empty string
JSON.parse("");
JSON.parse(" ");Both fail because the parser never sees a complete JSON value. This is one of the most common reasons for the error.
2. Truncated object or array
{
"user": "Ana",
"roles": ["admin", "editor"],
"settings": {
"theme": "dark"The input stops before the inner object and outer object are closed, so the parser reaches the end early.
3. Empty API response parsed as JSON
const response = await fetch("/api/session");
const data = await response.json();This commonly breaks when the server returns no body for a no-content case, or when the response is cut off before the JSON finishes.
Important distinction
If the response is HTML instead of JSON, you usually get a different error such as Unexpected token <. The Unexpected end message is a stronger signal that the body is empty or incomplete.
How to fix it
1. Inspect the raw input before parsing
Before you change anything else, log the actual string you are parsing, its length, and the last part of the payload. That tells you quickly whether the problem is an empty body or a truncated one.
console.log("length:", text.length);
console.log("tail:", text.slice(-200));
const data = JSON.parse(text);2. Guard against empty HTTP responses
If the payload comes from fetch(), use response.text() while debugging so you can inspect the raw body. Then parse it yourself only when there is something to parse.
Safer parsing pattern
async function readJsonOrNull(response) {
if (response.status === 204 || response.status === 304) {
return null;
}
const contentType = response.headers.get("content-type") || "";
const text = await response.text();
if (!text.trim()) {
return null;
}
if (!contentType.includes("application/json")) {
throw new Error(`Expected JSON but received ${contentType || "unknown content type"}`);
}
return JSON.parse(text);
}For HEAD requests, skip JSON parsing entirely because the response body is intentionally empty.
3. Fix truncated responses at the source
If the payload ends halfway through an object or array, the bug is usually not in JSON.parse(). It is in the code or infrastructure producing the response.
- Check the browser Network panel and compare the response preview with the raw response body.
- Log the serialized JSON length on the server before sending it.
- Look for timeouts, reverse-proxy limits, or middleware that may cut off large responses.
- If the response is compressed, test once without compression to rule out broken transfer or decoding.
- Do not build JSON by hand. Serialize plain objects with your platform's JSON encoder.
4. Repair incomplete JSON files or strings
If the JSON came from a file, clipboard, or manual edit, open it in a formatter or validator and look near the end of the document. The missing character is often close to the last visible line.
Before
{
"items": [
{ "id": 1 },
{ "id": 2 }
After
{
"items": [
{ "id": 1 },
{ "id": 2 }
]
}A quick debugging checklist
- Print the raw input and confirm it is not empty.
- Check whether the last characters look cut off mid-string, mid-object, or mid-array.
- Verify the HTTP status code before parsing, especially for
204and304. - Verify the
content-typeheader really says JSON. - Switch from
response.json()toresponse.text()until you see the real payload. - If it only happens on large responses, investigate truncation, streaming, or proxy limits.
Prevention strategies
- Prefer
JSON.stringify()over manual string concatenation when producing JSON. - Handle empty or no-content responses explicitly in your API client.
- Validate user-supplied JSON before saving or parsing it.
- Log response status, headers, and body length around JSON parsing failures.
- Use a formatter when editing JSON by hand so mismatched braces are obvious.
Conclusion
In most cases, Unexpected end of JSON input is not a mysterious parser bug. It is a clue that the parser received too little data. Start by checking whether the input is empty, then whether the response was truncated, and only then look for syntax mistakes in the JSON itself.
Once you inspect the raw text instead of the parsed result, this error usually becomes straightforward to diagnose and fix.
Need help with your JSON?
Try our JSON Formatter tool to automatically identify and fix syntax errors in your JSON. JSON Formatter tool