Introduction
JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write, and easy for machines to parse and generate. It’s commonly used for transmitting data in web applications, storing configuration files, and more. Python provides built-in support for working with JSON through the json module. This tutorial will cover how to serialize (convert) Python dictionaries into JSON format and write them to files.
The json Module
The json module is your primary tool for working with JSON data in Python. It provides functions for both encoding Python objects into JSON strings (serialization) and decoding JSON strings into Python objects (deserialization).
Serializing a Dictionary to a JSON String
The json.dumps() function takes a Python object (like a dictionary) and returns a JSON formatted string.
import json
data = {'name': 'Alice', 'age': 30, 'city': 'New York'}
json_string = json.dumps(data)
print(json_string)
# Output: {"name": "Alice", "age": 30, "city": "New York"}
As you can see, the dictionary data has been converted into a JSON string.
Writing JSON to a File
To write the JSON string to a file, you’ll open the file in write mode ('w') and write the string to it.
import json
data = {'name': 'Bob', 'age': 25, 'city': 'London'}
with open('data.json', 'w') as f:
json.dump(data, f)
This code will create a file named data.json (or overwrite it if it already exists) and write the JSON representation of the data dictionary to the file.
Pretty-Printing JSON
For human readability, you can format the JSON output with indentation. The indent parameter in json.dump() controls the number of spaces used for indentation.
import json
data = {'name': 'Charlie', 'age': 40, 'city': 'Paris'}
with open('data_pretty.json', 'w') as f:
json.dump(data, f, indent=4)
This will create a data_pretty.json file with a nicely formatted JSON structure:
{
"name": "Charlie",
"age": 40,
"city": "Paris"
}
Handling Complex Data Structures
The json module can handle more complex data structures like lists and nested dictionaries.
import json
data = {
'name': 'David',
'age': 35,
'city': 'Tokyo',
'skills': ['Python', 'JavaScript', 'Data Analysis']
}
with open('data_complex.json', 'w') as f:
json.dump(data, f, indent=2)
This will correctly serialize the dictionary with the list of skills into a JSON file.
Customizing Serialization with default
If your dictionary contains objects that are not directly serializable by the json module (e.g., custom class instances), you can provide a default function to handle the serialization of those objects. This function should take an object as input and return a serializable representation of it.
import json
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def to_dict(self):
return {'name': self.name, 'age': self.age}
def default_handler(obj):
if isinstance(obj, Person):
return obj.to_dict()
raise TypeError(f"Object of type {obj.__class__.__name__} is not JSON serializable")
person = Person("Eve", 28)
with open('person.json', 'w') as f:
json.dump(person, f, default=default_handler, indent=4)
In this example, the default_handler function checks if the object is an instance of the Person class. If it is, it calls the to_dict() method to convert the object into a dictionary, which can then be serialized by the json module.
Key Considerations
- Data Types: JSON supports a limited set of data types: numbers, strings, booleans, null, lists, and dictionaries. Ensure that your Python data structures can be mapped to these types.
- File Handling: Always use
with open(...)to ensure that files are properly closed, even if errors occur. - Error Handling: Consider using
try...exceptblocks to handle potentialTypeErrorexceptions that may occur if you try to serialize an unsupported data type.