json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
The string youβre trying to parse isnβt valid JSON.
Fix 1: Check the response
import requests
res = requests.get('https://api.example.com/data')
# β Response might not be JSON
data = res.json()
# β
Check first
if res.status_code == 200:
data = res.json()
else:
print(f"Error {res.status_code}: {res.text}")
Fix 2: Empty string
import json
# β Empty string isn't valid JSON
json.loads('')
# β
Check for empty
text = response.text
if text:
data = json.loads(text)
Fix 3: Invalid JSON format
# β Single quotes aren't valid JSON
json.loads("{'name': 'Alice'}")
# β
Use double quotes
json.loads('{"name": "Alice"}')
Related resources
Related: Pip Install Error Fix