Files & data
Medium
Parse JSON safely
Load JSON from a string and handle decode errors without crashing.
How it works
json.loads parses a JSON string into Python objects — dicts, lists, strings, numbers, booleans, and null.
Wrap parsing in try/except json.JSONDecodeError when input comes from users or external APIs.
Use json.dumps with indent=2 when you need pretty-printed output for debugging.
import json
payload = '{"name": "Ada", "skills": ["Python", "math"]}'
try:
data = json.loads(payload)
print(data["name"])
print(", ".join(data["skills"]))
except json.JSONDecodeError as exc:
print(f"Invalid JSON: {exc}")
Sponsored
Sponsored
Reserved space — layout preview until AdSense is connected