Unexpected End of JSON Input: Causes and Fixes
This error means the parser reached the end of the text before finding complete JSON. The fix is almost always upstream: find out why the text was empty or cut off, then handle that case in code.
What the error means
In JavaScript, JSON.parse throws Unexpected end of JSON input when the text ends before the JSON is complete. The parser was still expecting more characters, such as a closing bracket or a value, and found nothing. An empty string fails the same way, because it contains no JSON at all.
The wording comes from the JavaScript engine. In Python, the same situation raises a different message: Expecting value: line 1 column 1 (char 0) for empty input. The cause is the same in both languages: the text handed to the parser was empty or cut off, so the fix lies in finding why the text was incomplete.
Paste suspect text into the JSON validator on this site to check whether it is complete and valid. The JSON formatter on MixKhazana offers a second check with formatting.
The five most common causes
Almost every case traces to one of these five causes:
- Truncated response: the connection closed, a proxy cut the body, or a size limit stopped the download partway through. The text ends mid-object, mid-array or mid-string.
- Empty body: the server returned no body at all, as with a 204 No Content response. Parsing an empty string always fails.
- Wrong content type or error page: the server returned an error status with an empty body, a login page or plain text instead of JSON, often with a 404 or 500 status. An empty error body produces this exact error; an HTML page usually produces a different message naming the "<" character — either way, checking the raw text identifies it.
- An empty string from a variable that was never filled: code parses a value that turned out to be an empty string — for example, reading from storage or state that was never set, or taking an empty piece from split text. Parsing an empty string always throws this error. (Parsing a value that is already an object is a different mistake with a different message: if you already hold an object, use it directly instead of parsing it.)
- File cut off: a file was only partly written, partly downloaded or truncated by an interrupted save. The JSON on disk ends before its closing brackets, so parsing it fails until the file is replaced with a complete copy.
How to find which cause applies
Check the raw text before changing parsing code: it shows at once whether the body is empty, cut off, or HTML.
- Read the response as text first, instead of parsing it directly, and log or print the first few hundred characters.
- Check the status code. An error status with an HTML body points to a server or routing problem, not a parsing problem.
- Check the content type header. A JSON response should declare a JSON content type; an HTML content type means the body is a web page, not data.
- Compare the length of the text with what you expect. Text that ends mid-word or mid-string was truncated.
- Paste the raw text into the JSON validator to confirm where it breaks, then fix the source of the text, not the parser call.
Do not log full bodies that may contain passwords, tokens or personal data; the first characters and the length usually identify the cause.
Fixes in JavaScript and Node.js
In browser code, read the body as text, reject empty bodies early, and parse inside error handling that keeps the raw text for diagnosis:
const response = await fetch(url);
const text = await response.text();
if (!text.trim()) {
throw new Error("Empty response body");
}
let data;
try {
data = JSON.parse(text);
} catch (error) {
console.error("Invalid JSON received, length " + text.length + ":", text.slice(0, 200));
throw error;
}Calling response.json() directly on an empty body fails the same way, so the text-first pattern is safer.
In Node.js, the same pattern applies to files. Read the file as text, check that it is not empty, and only then parse it:
const fs = require("fs");
const text = fs.readFileSync("data.json", "utf8");
if (!text.trim()) {
throw new Error("File is empty");
}
const data = JSON.parse(text);If the file is cut off, no parsing fix will help: re-download or regenerate the complete file and check its size before parsing.
Fixes in Python
Python raises json.JSONDecodeError when parsing fails, with the message Expecting value: line 1 column 1 (char 0) for empty input. Read the text first and check it before parsing:
import json
with open("data.json", encoding="utf-8") as f:
text = f.read()
if not text.strip():
raise ValueError("File is empty")
data = json.loads(text)For HTTP responses with the requests library, check the status and the body text before asking for JSON:
import requests
response = requests.get(url)
response.raise_for_status()
if not response.text.strip():
raise ValueError("Empty response body")
data = response.json()Wrap parsing in error handling that reports the text length and first characters, so a truncated or HTML body is recognised at once.
Prevention checklist
- On the server: always return either valid JSON with a JSON content type, or a proper error status with a clear body. Never return an empty body from an endpoint that clients parse as JSON.
- On the client: check the status, content type and body length before parsing, and handle empty bodies as a normal case.
- In code: keep parsing inside error handling, and log the length and first characters of bad input, without secrets, so the cause is visible in logs.
- In tests: include empty, truncated and HTML bodies as test cases.
- For files: write files completely before they are read, verify downloads by size where possible, and keep a known-good copy.
Frequently asked questions
What does Unexpected end of JSON input mean in plain English?
It means the parser ran out of characters before the JSON was complete. The text was empty, or it stopped in the middle of an object, array or string.
Why does fetch fail even when the server returns a success status?
A success status does not guarantee a JSON body: the response may be empty or replaced. Reading the body as text first shows what actually arrived.
Is this a bug in JSON.parse?
No. The parser is correctly reporting that its input is incomplete. The bug is upstream, in whatever produced, delivered or stored the text.
How do I see the raw response text?
In JavaScript, use response.text() and log the result before parsing. In Python, print response.text or the file contents before calling json.loads. Check the first characters and the length first.
Why does Python show a different error message?
Each language has its own parser and wording. Python reports Expecting value: line 1 column 1 (char 0) for empty input, while JavaScript engines report Unexpected end of JSON input. The underlying cause is the same.
Can a validator fix this error?
No, a validator only shows whether text is valid and where it breaks. The fix must happen where the text comes from: the server, the request, the file, or the code that reads the response.



