🔧 Error Fixes
· 1 min read

Python JSONDecodeError — How to Fix It


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"}')
📘