All lessons
πŸ“‹

Working with JSON

Parse and create JSON data

Reward
+40 XP

JSON in Python

The json module converts between Python dicts/lists and JSON strings.

import json

# Python dict to JSON string
data = {"name": "Alice", "age": 30}
json_str = json.dumps(data)
print(json_str)

Parsing JSON

Use json.loads() to parse a JSON string back to Python objects.

import json
json_str = '{"x": 1, "y": 2}'
data = json.loads(json_str)
print(data["x"])

Try it yourself

Run the example below. Edit it and see what changes.

main.py
Output
Press Run to execute (Ctrl/Cmd+Enter)

Common mistakes

  • ⚠JSON requires double quotes for strings β€” single quotes cause errors.
  • ⚠Confusing dumps/loads (strings) with dump/load (files).