Answer key · 12 questions

Python JSON for Beginners Exercises With Answers

All 12 questions for Python JSON for Beginners in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

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.

01 · Multiple choice

Which function parses a JSON string into Python values?

  1. json.parse
  2. json.read
  3. json.dumps
  4. json.loadscorrect

Answer: json.loads

Why: loads stands for load string; it parses JSON text.

02 · Multiple choice

Which function turns a Python dictionary into JSON text?

  1. json.encode
  2. json.loads
  3. json.dumpscorrect
  4. json.write

Answer: json.dumps

Why: dumps stands for dump string; it serialises a value to JSON text.

03 · Multiple choice

What Python type does a JSON object become?

  1. A dictionarycorrect
  2. A list
  3. An integer
  4. A string

Answer: A dictionary

Why: JSON objects have named keys, so they map naturally to dictionaries.

04 · Multiple choice

What does json.loads('{"a": 1}')["a"] return?

  1. None
  2. "a"
  3. A list
  4. 1correct

Answer: 1

Why: The JSON object becomes a dictionary, and key a holds the number 1.

05 · Fill in the blank

Complete the import statement for Python's JSON module.

import ____

Answer: json

Why: The module is imported with import json.

06 · Fill in the blank

Complete the function that parses JSON text.

data = json.____(text)

Answer: loads

Why: json.loads parses a string containing JSON.

07 · Fill in the blank

Complete the function that serialises a value to JSON text.

text = json.____(data)

Answer: dumps

Why: json.dumps returns a JSON-formatted string.

08 · Fill in the blank

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.

09 · Predict output

What appears on the screen?

import json
print(json.loads('{"a": 1}')["a"])

Answer: 1

Why: The parsed dictionary stores 1 under key a.

10 · Predict output

What is printed?

import json
print(json.dumps({"a": 1}))

Answer: {"a": 1}

Why: dumps returns compact JSON text with double quotes.

11 · Predict output

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.

12 · Predict output

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.

Practice this lesson