📖 Scenario: You are working on a simple contact list app. You want to save the contacts to a file and later read them back.
🎯 Goal: Learn how to convert a Python dictionary to a JSON string (serialize) and then convert it back from JSON string to a Python dictionary (deserialize).
📋 What You'll Learn
Create a Python dictionary with exact contact details
Create a JSON string from the dictionary using json.dumps()
Convert the JSON string back to a dictionary using json.loads()
Print the final dictionary to verify it matches the original
💡 Why This Matters
🌍 Real World
Many apps save data in JSON format because it is easy to share and read by different programs.
💼 Career
Understanding JSON serialization and deserialization is essential for backend development, APIs, and data exchange between systems.
Progress0 / 4 steps
1
Create the contacts dictionary
Create a dictionary called contacts with these exact entries: 'Alice': 'alice@example.com', 'Bob': 'bob@example.com', 'Charlie': 'charlie@example.com'
Python
Hint
Use curly braces {} to create a dictionary and separate keys and values with colons.
2
Import the json module
Add the line import json at the top of your code to use JSON functions
Python
Hint
Use import json to access JSON functions in Python.
3
Serialize the dictionary to JSON string
Create a variable called contacts_json and set it to the JSON string of contacts using json.dumps(contacts)
Python
Hint
Use json.dumps() to convert a dictionary to a JSON string.
4
Deserialize the JSON string and print
Create a variable called contacts_loaded and set it to the dictionary obtained by deserializing contacts_json using json.loads(contacts_json). Then print contacts_loaded
Python
Hint
Use json.loads() to convert JSON string back to a dictionary. Then use print() to show the result.
Practice
(1/5)
1. What does the json.dumps() function do in Python?
easy
A. Reads JSON data from a file
B. Converts Python data into a JSON formatted string
C. Converts JSON string back to Python data
D. Writes Python data directly to a file
Solution
Step 1: Understand the purpose of json.dumps()
This function takes Python objects like dictionaries or lists and turns them into a JSON string.
Step 2: Differentiate from other JSON functions
json.loads() converts JSON strings back to Python objects, while dumps() does the opposite.
Final Answer:
Converts Python data into a JSON formatted string -> Option B
Quick Check:
Serialize = Python to JSON string [OK]
Hint: Remember: dumps() means dump Python to JSON string [OK]
Common Mistakes:
Confusing dumps() with loads()
Thinking dumps() writes to a file
Assuming dumps() reads JSON data
2. Which of the following is the correct syntax to deserialize a JSON string json_str into a Python object?
easy
A. json.load(json_str)
B. json.dumps(json_str)
C. json.loads(json_str)
D. json.deserialize(json_str)
Solution
Step 1: Identify the function to convert JSON string to Python
The correct function is json.loads(), which takes a JSON string and returns Python data.
Step 2: Check other options for correctness
json.dumps() serializes Python to JSON string, json.load() reads JSON from a file object, and json.deserialize() does not exist.
Final Answer:
json.loads(json_str) -> Option C
Quick Check:
loads() = JSON string to Python [OK]
Hint: Use loads() to load JSON string into Python [OK]
A. json.load() expects a file object, not a string
B. json_str is not a valid JSON string
C. json.loads() should be replaced with json.dumps()
D. Missing import statement for json module
Solution
Step 1: Understand the difference between json.load() and json.loads()
json.load() reads JSON data from a file-like object, not from a string.
Step 2: Identify correct function for string input
To convert a JSON string to Python data, use json.loads() instead of json.load().
Final Answer:
json.load() expects a file object, not a string -> Option A
Quick Check:
load() = file, loads() = string [OK]
Hint: Use loads() for strings, load() for files [OK]
Common Mistakes:
Using load() on a string instead of loads()
Assuming json_str is invalid JSON
Confusing dumps() and loads()
5. You have a Python list data = [{'id': 1}, {'id': 2}, {'id': 3}]. You want to serialize it to JSON but only include dictionaries where id is greater than 1. Which code correctly does this?
hard
A. json.dumps([item for item in data if item['id'] > 1])
B. json.dumps(data.filter(lambda x: x['id'] > 1))
C. json.dumps(filter(lambda x: x['id'] > 1, data))
D. json.dumps([item for item in data if item.id > 1])
Solution
Step 1: Filter list with list comprehension
Use a list comprehension to select dictionaries where id is greater than 1: [item for item in data if item['id'] > 1].
Step 2: Serialize filtered list to JSON string
Pass the filtered list to json.dumps() to get the JSON string.
Final Answer:
json.dumps([item for item in data if item['id'] > 1]) -> Option A
Quick Check:
Filter with list comprehension, then dumps() [OK]
Hint: Filter with list comprehension before dumps() [OK]
Common Mistakes:
Using filter() without converting to list
Trying to access dict keys with dot notation
Using filter() result directly without list()
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.