Python cannot decode the response because the body is empty or HTML
Your error
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)1 more line it can showFix it fast
Most common cause, 1 of 3The server sent an HTML page instead of JSON
Stop on 4xx and 5xx replies before decoding, and refuse anything that is not labelled as JSON.
import requests
r = requests.get(url, headers={"Accept": "application/json"}, timeout=10)
r.raise_for_status()
if "application/json" not in r.headers.get("Content-Type", ""):
raise RuntimeError(f"Not JSON ({r.headers.get('Content-Type')}): {r.text[:200]}")
data = r.json()Short answer
JSONDecodeError: Expecting value: line 1 column 1 (char 0) means json.loads() or requests' r.json() got text whose first character is not JSON: an empty body, a 204 reply, an empty file, or an HTML error page. Print r.status_code and r.text[:200], call r.raise_for_status() before decoding, and skip empty bodies. Applies to Python 3.10 to 3.15 and requests 2.x.
Which cause do you have?
Look at what actually came back before you decode it. A body that starts with < points to cause 1. A length of 0 points to cause 2. If the traceback ends in json.load on a file and not in r.json(), go to cause 3.
python -c "import requests; r = requests.get('https://api.example.com/items', timeout=10); print(r.status_code, r.headers.get('Content-Type'), len(r.content)); print(r.text[:200])"Then find the line you see in this list and open its fix:
- 1The server sent an HTML page instead of JSON
The printed body begins with <!DOCTYPE html> or <html, and Content-Type is text/html; the status is often 401, 403, 404 or 500 - 2The reply has no body at all
len(r.content) prints 0, often with status 204 - 3json.load() read an empty file, or a file handle already at its end
The traceback ends in json.load or json.loads on a file, the file size is 0, or the same handle was passed to f.read() earlier
What does the error look like?
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Cause 1 of 3
The server sent an HTML page instead of JSON
You seeThe printed body begins with <!DOCTYPE html> or <html, and Content-Type is text/html; the status is often 401, 403, 404 or 500
Check
r.json() fails with Expecting value at char 0 when the reply is an HTML page, because < cannot start a JSON document. A server error page or a sign-in page is the usual source. The requests guide adds a warning: decoding can succeed on a failed response too, so the status code needs its own check.
r = requests.get(url, timeout=10)
print(r.status_code, r.headers.get("Content-Type"))
print(r.text[:200])Fix
Stop on 4xx and 5xx replies before decoding, and refuse anything that is not labelled as JSON. Then solve what the HTML page reports, which is usually a wrong URL, a missing or expired token, or a crash on the server.
import requests
r = requests.get(url, headers={"Accept": "application/json"}, timeout=10)
r.raise_for_status()
if "application/json" not in r.headers.get("Content-Type", ""):
raise RuntimeError(f"Not JSON ({r.headers.get('Content-Type')}): {r.text[:200]}")
data = r.json()Verify
A bad call now stops with requests.exceptions.HTTPError naming the status, or with your RuntimeError showing the page start. A good call returns parsed data.
python fetch_items.pyConfirmed by requests.readthedocs.io
Cause 2 of 3
The reply has no body at all
You seelen(r.content) prints 0, often with status 204
Check
With nothing to read, the decoder stops at position 0 and reports the same error. The requests documentation names a 204 No Content reply as one case where r.json() raises requests.exceptions.JSONDecodeError. A 204 is a success; it simply carries nothing.
print(r.status_code, len(r.content))Fix
Decode only when bytes arrived, and give the caller None (or an empty dict) for an empty success.
r.raise_for_status()
data = r.json() if r.content else NoneVerify
The same call returns None for the empty reply and the program carries on. Endpoints that send JSON still decode as before.
python fetch_items.pyConfirmed by requests.readthedocs.io
Cause 3 of 3
json.load() read an empty file, or a file handle already at its end
You seeThe traceback ends in json.load or json.loads on a file, the file size is 0, or the same handle was passed to f.read() earlier
Check
The standard json module raises JSONDecodeError for any text that is not a valid JSON document, and an empty string fails at char 0. The Python tutorial explains why a second read is empty: once f.read() reaches the end of a file, further calls return ''.
ls -l config.jsonFix
Read the file one time into a string, then decode that string only when it holds more than whitespace. Fix whatever wrote the empty file if it should never be empty.
import json
with open("config.json", encoding="utf-8") as f:
text = f.read()
data = json.loads(text) if text.strip() else {}Verify
An empty config.json gives {} without an exception, and a filled one loads its values.
python load_config.pyConfirmed by docs.python.org
How do I stop it coming back?
- Route every HTTP call through one helper that runs raise_for_status() and checks Content-Type, so no caller decodes an unchecked reply.
- Catch requests.exceptions.JSONDecodeError around r.json() and log the status code plus the first 200 characters of r.text.
- Keep a unit test that feeds an empty string and a short HTML page to your parsing code and expects a clear error.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.