2.1 The json Module
JSON (JavaScript Object Notation) is a lightweight data interchange format that's easy for humans to read and write and easy for machines to parse and generate. JSON is often used for transmitting data between a server and a web application, as well as for storing configurations and settings.
The json module in Python provides functions for serialization (converting Python objects to JSON strings) and deserialization (converting JSON strings to Python objects). This module is built into Python's standard library and is widely used to work with JSON data.
The main functions of the json module are very similar to those of pickle and work similarly. What can I say — it's the standard!
- Working with strings:
-
json.dumps(obj)— Converts a Python object to a JSON string. -
json.loads(s)— Converts a JSON string to a Python object.
-
- Working with files:
-
json.dump(obj, file)— Serializes a Python object and writes it to a text file. -
json.load(file)— Deserializes a Python object from a text file containing JSON data.
-
Let's go through a couple of examples to better remember how these functions work.
2.2 Serialization to a String
Serializing Python objects to JSON strings
To serialize an object to a string, just pass it to the json.dumps() function.
import json
# Example object for serialization
data = {
"name": "Alice",
"age": 30,
"is_student": False,
"courses": ["Math", "Science"],
"address": {
"city": "New York",
"zip_code": "10001"
}
}
# Convert Python object to JSON string
json_string = json.dumps(data, indent=4)
print("Serialized data (JSON):", json_string)
Deserializing JSON strings to Python objects
To get an object from a string, just pass the JSON string, containing the object's description, to the json.loads() method.
import json
# Example JSON string for deserialization
json_string = ''' { "name": "Alice", "age": 30, "is_student": false, "courses": ["Math", "Science"], "address": { "city": "New York", "zip_code": "10001" } } '''
# Convert JSON string to Python object
data = json.loads(json_string)
print("Deserialized data (Python):", data)
2.3 Serialization to a File
Writing Python objects to a file in JSON format
To write an object to a file, call the json.dump() method. When working with files, it's important to use exception handling to properly manage potential errors. Here's an example:
import json
# Example object for serialization
data = {
"name": "Bob",
"age": 25,
"is_student": True,
"courses": ["History", "Literature"],
"address": {
"city": "Los Angeles",
"zip_code": "90001"
}
}
# Write Python object to JSON file with exception handling
try:
with open('data.json', 'w') as file:
json.dump(data, file, indent=4)
print("Data successfully written to file.")
except IOError:
print("Error writing to file.")
except json.JSONEncodeError:
print("Error encoding JSON.")
Reading Python objects from a file in JSON format
Reading is also very straightforward — just pass the file to the json.load() method. Again, it's crucial to use exception handling:
import json
# Read Python object from JSON file with exception handling
try:
with open('data.json', 'r') as file:
data = json.load(file)
print("Deserialized data from file (Python):", data)
except IOError:
print("Error reading file.")
except json.JSONDecodeError:
print("Error decoding JSON.")
2.4 Additional Function Parameters
You can pass additional parameters to the serialization function to make your JSON prettier:
-
skipkeys: IfTrue, skips keys that are not strings, numbers, orNone. -
ensure_ascii: IfTrue, all non-ASCII characters will be escaped using Unicode escape sequences. -
indent: If a number is specified, indents will be added for better readability. -
sort_keys: IfTrue, the keys in the JSON will be sorted alphabetically.
Example of using json.dumps() parameters:
import json
data = {"c": 3, "b": 2, "a": 1}
# Serialization with sorted keys and indents
json_string = json.dumps(data, indent=4, sort_keys=True)
print(json_string)
2.5 Custom Encoders and Decoders
The json module allows you to use custom functions for serializing and deserializing objects.
Example of a custom encoder
First, we create a special encoder class that internally checks that if the object's type == datetime, then it returns the object as an ISO format string.
import json
from datetime import datetime
class CustomEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime): return obj.isoformat()
return super().default(obj)
data = {
"name": "Alice",
"timestamp": datetime.now()
}
# Serialization with custom encoder
json_string = json.dumps(data, cls=CustomEncoder, indent=4)
print(json_string)
Example of a custom decoder
For the reverse operation, we also need a function that converts a string to a date. For example, we can simply check the field name, and if it's called timestamp, then convert the string into a datetime object:
import json
from datetime import datetime
def custom_decoder(dct):
if 'timestamp' in dct: dct['timestamp'] = datetime.fromisoformat(dct['timestamp'])
return dct
json_string = '''
{
"name": "Alice",
"timestamp": "2023-05-15T14:30:00"
}
'''
# Deserialization with custom decoder
data = json.loads(json_string, object_hook=custom_decoder)
print(data)
GO TO FULL VERSION