We use JSON to save or send data in a simple text format. Serializing turns data into JSON text. Deserializing turns JSON text back into data.
Saving user settings to a file so they can be loaded later.
Sending data from a web app to a server.
Reading data from a file that was saved in JSON format.
Sharing data between different programs or devices.
Syntax
Python
import json
# Serialize (convert data to JSON string)
json_string = json.dumps(data)
# Deserialize (convert JSON string back to data)
data = json.loads(json_string)
json.dumps() converts Python data to a JSON string.
json.loads() converts a JSON string back to Python data.
Examples
This turns a Python dictionary into a JSON string.
Python
import json
# Serialize a dictionary to JSON string
data = {'name': 'Alice', 'age': 30}
json_str = json.dumps(data)
print(json_str)
This turns a JSON string back into a Python dictionary.
Python
import json
# Deserialize JSON string back to dictionary
json_str = '{"name": "Alice", "age": 30}'
data = json.loads(json_str)
print(data)
Lists can also be converted to JSON strings.
Python
import json
# Serialize a list to JSON string
numbers = [1, 2, 3, 4]
json_str = json.dumps(numbers)
print(json_str)
Sample Program
This program shows how to convert a Python dictionary to a JSON string and back again.
Python
import json
# Original data
person = {'name': 'Bob', 'age': 25, 'city': 'New York'}
# Serialize to JSON string
json_data = json.dumps(person)
print('Serialized JSON:', json_data)
# Deserialize back to Python dictionary
person_data = json.loads(json_data)
print('Deserialized data:', person_data)
OutputSuccess
Important Notes
JSON only supports basic data types like strings, numbers, lists, and dictionaries.
Python objects like sets or custom classes need special handling before serializing.
Use indent parameter in json.dumps() to make JSON output easier to read.
Summary
Serializing means converting Python data to JSON text.
Deserializing means converting JSON text back to Python data.
Use the json module's dumps() and loads() functions for this.
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.