Syncing account progress…

Back

Intermediate · 12 min

Working with JSON

Convert between JSON text and Python data.

Parse JSON text

json.loads() converts JSON text into Python values. JSON objects become dictionaries.

import json
text = '{"name": "Ada", "active": true}'
profile = json.loads(text)
print(profile["active"])

Output

True

Create JSON text

json.dumps() serializes Python data. sort_keys=True makes object key order predictable.

import json
data = {"score": 9, "name": "Mina"}
print(json.dumps(data, sort_keys=True))

Output

{"name": "Mina", "score": 9}

Working with JSON

  1. Read both working with json examples before answering.
  2. Run the task, compare its exact output, and revise the code if needed.

Try it yourself

Code runs on this device. When you are signed in, drafts sync to your account.

Read an API response

Parse response with json.loads(), calculate the sum of its scores, and display Total: 15.

Practice