This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python JSON for Beginners, with the exact answer and a short explanation for every question.
The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.
Which function parses a JSON string into Python values?
Answer: json.loads
Why: loads stands for load string; it parses JSON text.
Which function turns a Python dictionary into JSON text?
Answer: json.dumps
Why: dumps stands for dump string; it serialises a value to JSON text.
What Python type does a JSON object become?
Answer: A dictionary
Why: JSON objects have named keys, so they map naturally to dictionaries.
What does json.loads('{"a": 1}')["a"] return?
Answer: 1
Why: The JSON object becomes a dictionary, and key a holds the number 1.
Complete the import statement for Python's JSON module.
import ____Answer: json
Why: The module is imported with import json.
Complete the function that parses JSON text.
data = json.____(text)Answer: loads
Why: json.loads parses a string containing JSON.
Complete the function that serialises a value to JSON text.
text = json.____(data)Answer: dumps
Why: json.dumps returns a JSON-formatted string.
Complete the key that contains the list of records.
for item in data["____"]:
print(item)Answer: items
Why: The example JSON stores its records under the key items.
What appears on the screen?
import json
print(json.loads('{"a": 1}')["a"])Answer: 1
Why: The parsed dictionary stores 1 under key a.
What is printed?
import json
print(json.dumps({"a": 1}))Answer: {"a": 1}
Why: dumps returns compact JSON text with double quotes.
What is the output?
import json
data = json.loads('{"items": [2, 4]}')
print(data["items"][1])Answer: 4
Why: Index 1 selects the second item in the list.
What does this code print?
import json
data = {"items": [1, 2, 3]}
print(sum(data["items"]))Answer: 6
Why: sum adds the three values in the list.
Try the questions first
Open the practice page for Python JSON for Beginners and answer the same 12 questions with instant feedback.