/errors/json/expecting-value-line-1-column-1
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
- tool
- JSON
- exit code
- 1
- versions
- Python 3.5+
- os
- any
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) means the JSON parser found no JSON value at the very first character. The input is either empty or starts with something that is not JSON, most often an HTML page. The fix is to look at what you actually received (status code, headers, raw text) before calling json.loads() or response.json().
Reproduce it
cat > parse.py <<'EOF'
import json
for raw in ["", "<!DOCTYPE html><html>502 Bad Gateway</html>", "\ufeff{}", "{'a': 1}", '{"a": 1'] :
try:
json.loads(raw)
print("ok ", repr(raw))
except json.JSONDecodeError as e:
print(f"{type(e).__name__}: {e} <- {raw[:20]!r}")
EOF
python parse.pyOutput
JSONDecodeError: Expecting value: line 1 column 1 (char 0) <- ''
JSONDecodeError: Expecting value: line 1 column 1 (char 0) <- '<!DOCTYPE html><html'
JSONDecodeError: Unexpected UTF-8 BOM (decode using utf-8-sig): line 1 column 1 (char 0) <- '\ufeff{}'
JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1) <- "{'a': 1}"
JSONDecodeError: Expecting ',' delimiter: line 1 column 8 (char 7) <- '{"a": 1'Only the first two inputs give Expecting value: line 1 column 1 (char 0); the others fail with their own messages, which point at the actual problem (a BOM, single quotes, truncated input).
Debug a failing API call
resp = requests.get(url, timeout=10)
print(resp.status_code, resp.headers.get("Content-Type"))
print(resp.text[:200]) # look at the start of the body
resp.raise_for_status() # stop on 4xx/5xx before parsing
data = resp.json() if resp.content else Nonerequests raises requests.exceptions.JSONDecodeError with the same message, and it is a subclass of the standard json.JSONDecodeError, so except json.JSONDecodeError catches both.
| Message | Usual cause |
|---|---|
| Expecting value: line 1 column 1 (char 0) | Empty body or HTML instead of JSON |
| Unexpected UTF-8 BOM (decode using utf-8-sig) | File or response starts with a BOM |
| Expecting property name enclosed in double quotes | Single quotes or a trailing comma (Python dict syntax, not JSON) |
| Expecting ',' delimiter | Truncated or concatenated JSON |
| Extra data | Two JSON documents in one string (JSON Lines needs one loads() per line) |