Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

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 show

Fix it fast

Most common cause, 1 of 3

The 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.

Fix
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()
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to Python 3.15.2, Python 3.14, requests 2.34.2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
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:

  1. 1The server sent an HTML page instead of JSONThe printed body begins with <!DOCTYPE html> or <html, and Content-Type is text/html; the status is often 401, 403, 404 or 500
  2. 2The reply has no body at alllen(r.content) prints 0, often with status 204
  3. 3json.load() read an empty file, or a file handle already at its endThe 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.

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.

Fix
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.

Verify
python fetch_items.py

Confirmed by requests.readthedocs.io

Did this fix it?

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.

Check
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.

Fix
r.raise_for_status()
data = r.json() if r.content else None

Verify

The same call returns None for the empty reply and the program carries on. Endpoints that send JSON still decode as before.

Verify
python fetch_items.py

Confirmed by requests.readthedocs.io

Did this fix it?

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 ''.

Check
ls -l config.json

Fix

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.

Fix
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.

Verify
python load_config.py

Confirmed by docs.python.org

Did this fix it?

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.

Free tool for this

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.