Unexpected end of JSON input: causes and fixes
This error means the parser ran out of text before the JSON was complete. Either there was no text at all, or the JSON stops partway through an object or array.
Most of the time the response was empty, for example when response.json() is called on a request that returned no body.
Quick fix
- Check the response status. A 204 No Content, or an error status, often has an empty body.
- Read the body with
response.text()and only callJSON.parseif the text isn’t empty. - If the text isn’t empty, check whether it ends before every
}and]is closed. If it does, fetch or export the data again.
Find and fix it in the validator
Paste your broken JSON into the JSON validator. It underlines the first error as you type, and Fix My JSON repairs common mistakes such as missing quotes and extra commas. It runs in your browser, so your JSON isn’t uploaded.
This example was cut off partway through. Fix My JSON closes the open brackets so you can read what’s there, but it can’t bring back anything that was lost.
{
"id": 42,
"name": "Maya",
"roles": ["editor", "admin"The message in each browser and language
You might see any of these, depending on where the JSON is parsed:
| Where | Message |
|---|---|
| Chrome, Edge, Node.js | Unexpected end of JSON input |
| Firefox | JSON.parse: unexpected end of data at line 1 column 1 of the JSON data |
| Safari | JSON Parse error: Unexpected EOF |
| Python | JSONDecodeError: Expecting value: line 1 column 1 (char 0) |
When the response or string is empty
- A 204 No Content response. Many APIs return 204 with no body for successful DELETE, PUT, or PATCH requests. Calling
response.json()on one always fails. - An error with no body. Some servers send a 401, 404, or 500 status with an empty body. Check
response.statusbefore reading the body. mode: "no-cors". This option returns an opaque response whose body is always empty, even when the server sent JSON. Remove the option and have the server send the right CORS headers instead.- An empty string.
JSON.parse("")fails the same way. Look for an empty form field, a file that hasn’t been written yet, or a stored value that was never set.JSON.parse(null)doesn’t fail; it returnsnull.
To handle responses that may be empty, read the text first and only parse it if there’s something there:
const response = await fetch(`/api/items/${id}`, { method: "DELETE" });
if (!response.ok) {
throw new Error(`Request failed with ${response.status}`);
}
const text = await response.text();
const data = text ? JSON.parse(text) : null;When the JSON is cut off
If the text isn’t empty, it probably ends before the last string, object, or array is closed. Common causes are a server that crashed or timed out partway through a large response, a log viewer that shortens long lines, copying only part of a document, and reading a file while another program is still writing it.
Chrome and Node.js don’t always say “unexpected end” for truncated JSON. If the text happens to end right after a value, you’ll get a message like Expected ',' or '}' after property value in JSON at position 42 instead. To check, compare that position with text.length. If they’re equal, the parser reached the end of the text, so the JSON was cut off.
Adding closing brackets to truncated data won’t bring back what’s missing, so fetch or export it again. When you build JSON yourself, use JSON.stringify rather than joining strings, so the brackets always match.
In Python, the equivalent errors are covered in Expecting value: line 1 column 1.
Still stuck? Paste your JSON into the validator to see exactly where the first error is, or try Fix My JSON.