JSON
Python · Unit 16: The standard library
- Full plan
- Python
- 10 exercises
JSON is the most common way to send data between programs, and it is nothing more than text in an agreed shape: it looks almost like a Python dictionary.
json.loads reads that text and turns it into real data you can work with.
import json
t = '{"name": "Amy", "age": 30}'
d = json.loads(t)
print(d["name"])Prints
Amy
The rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Predict the output
What does this code print?
2. Complete the code
Complete it to convert the text. It should print: 5
3. Predict the output
What does this code print?
4. Predict the output
What does this code print?
5. Complete the code
Complete it to turn the list into JSON text.
6. Find the bug
This program should print 5. Which line has the error?
7. Multiple choice
What does this program do?
8. Predict the output
JSON can carry lists inside. What does it print?
9. Put the lines in order
Put the lines in order to build
total(text), which adds up the price of every product in the JSON.10. Find the case that fails
read_n(text)returns the value of the key "n" in the JSON, or 0 if the text isn't valid JSON. Which call breaks it?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.