← Python from Scratch
Lesson 55 of 78 · 10 XP
JSON
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
JSON is a text format for exchanging data: it's how websites, apps and APIs talk to each other. It looks almost like Python dictionaries and lists:
{"name": "Zoë", "age": 20, "skills": ["Python", "C"], "admin": false}
The json module converts between text and Python objects:
import json
text = '{"name": "Zoë", "age": 20}'
data = json.loads(text) # string → dictionary
data["age"] # 20
json.dumps(data) # dictionary → string
json.dumps(data, ensure_ascii=False) # "Zoë" stays as is, not "Zo\u00eb"
json.dumps(data, indent=2) # with indentation, "pretty"
How the types match up: a JSON object → dict, an array → list, true/false → True/False, null → None.
For files there are json.load(f) and json.dump(data, f) — without the s at the end.
Task. Write a function adults(json_text). It receives a JSON array of people like [{"name": "Anya", "age": 17}, ...]. Return a list of the names of those who are 18 or older, in their original order.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.