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
- Raise for HTTP error status before reading the body as JSON.
- Guard against empty bodies and log the raw text on decode failure.
- 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
Python "yaml.scanner.ScannerError" in CIFix "yaml.scanner.ScannerError" in CI - PyYAML could not tokenize a YAML file, usually a tab character, bad i…
Python "configparser.NoSectionError" in CIFix "configparser.NoSectionError" in CI - configparser was asked for a section that the loaded .ini file does…
Python "requests.exceptions.ConnectionError: Max retries exceeded" in CIFix "requests.exceptions.ConnectionError: Max retries exceeded with url" in CI - requests could not establish…