Sponsored Reserved space — layout preview until AdSense is connected

Parse JSON safely

Load JSON from a string and handle decode errors without crashing.

10 lines 3 views 0 copies Updated May 23, 2026
Try in editor

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.

Python
Try in editor
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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Files & data