ProjectStack
python

json.decoder.JSONDecodeError: Expecting value

Python's json module tried to parse a string as JSON but found it wasn't valid JSON. The most common causes are an empty string, an HTML error page instead of JSON, or malformed JSON.

Common causes

  • An HTTP request returned an empty body, HTML error page, or plain text instead of JSON
  • The JSON string has a syntax error: trailing comma, unquoted keys, or single quotes
  • You called json.loads() on an empty string or None
  • The response was truncated or partially received

How to fix it

  1. Print the raw response before parsing to see what you actually received: print(response.text)
  2. Check the HTTP status code before parsing: if response.status_code == 200:
  3. Wrap in try/except: try: data = json.loads(text) except json.JSONDecodeError: ...
  4. Validate the JSON string at jsonlint.com if you're constructing it manually

Example

Traceback (most recent call last): File "app.py", line 6, in <module> data = json.loads(response.text) json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Parsing an API response that returned an empty string because the request failed

Have a different error?

Paste any error message into the Error Translator to get an instant explanation.