Skip to content
Latchkey

Python "json.decoder.JSONDecodeError: Expecting value" in CI

json.loads raises JSONDecodeError: Expecting value: line 1 column 1 (char 0) when handed an empty string or text that does not start with valid JSON, such as an HTML error page or a blank response body.

What this error means

A test or step fails with "json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)" right after reading a file or an HTTP response.

python
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Common causes

The input is empty or non-JSON

An API returned an empty body, an HTML error page, or a 204 No Content, and the code passed it straight to json.loads.

A response that was not checked for status

A failed request returned an error page; the code decoded the body without inspecting the status code.

How to fix it

Check status and content before decoding

  1. Raise for HTTP error status before reading the body as JSON.
  2. Guard against empty bodies and log the raw text on decode failure.
  3. When reading a file, confirm it is non-empty and well-formed first.
Python
resp.raise_for_status()
if not resp.content:
    raise RuntimeError("empty response body, expected JSON")
data = resp.json()

How to prevent it

  • Always check HTTP status before parsing the body.
  • Log the first bytes of the body when a JSON decode fails.
  • Mock external responses in tests so payloads are deterministic.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →