json
The Python json
module provides tools to encode and decode data in JavaScript Object Notation (JSON), a lightweight data interchange format that’s easy for humans to read and write and easy for machines to parse and generate.
Here’s a quick example:
>>> import json
>>> # Convert a Python dictionary to a JSON string
>>> json.dumps({"name": "Alice", "age": 30})
'{"name": "Alice", "age": 30}'
Key Features
Frequently Used Classes and Functions
Object | Type | Description |
---|---|---|
json.dump() |
Function | Serializes a Python object to a JSON-formatted stream |
json.dumps() |
Function | Serializes Python objects to a JSON-formatted string |
json.load() |
Function | Deserializes a JSON-formatted stream to a Python object |
json.loads() |
Function | Deserializes a JSON-formatted string to a Python object |
json.JSONEncoder |
Class | Provides a custom encoder for JSON serialization |
json.JSONDecoder |
Class | Provides a custom decoder for JSON deserialization |
Examples
Serializing a Python dictionary to a JSON string:
>>> import json
>>> data = {"name": "Bob", "age": 25}
>>> json_str = json.dumps(data)
>>> json_str
'{"name": "Bob", "age": 25}'
Deserializing a JSON string to a Python dictionary:
>>> json.loads('{"name": "Bob", "age": 25}')
{'name': 'Bob', 'age': 25}
Serializing with pretty printing:
>>> print(json.dumps(data, indent=2))
{
"name": "Bob",
"age": 25
}
Common Use Cases
- Storing and exchanging data between a web server and a client
- Configuring applications with JSON files
- Logging structured data in a human-readable format
Real-World Example
You can use the json
module to read configuration data from a JSON file and update it programmatically. Here’s an example of updating a user’s profile information stored in a JSON file:
>>> import json
>>> import pathlib
>>> config_file = pathlib.Path("user_config.json")
>>> # Read the existing configuration
>>> with config_file.open(mode="r", encoding="utf-8") as file:
... config = json.load(file)
...
>>> # Update user profile information
>>> config["user"]["name"] = "Charlie"
>>> config["user"]["preferences"]["theme"] = "dark"
>>> # Write the updated configuration back to the file
>>> with config_file.open(mode="w", encoding="utf-8") as file:
... json.dump(config, file, indent=4)
...
>>> # Verify the file update
>>> with config_file.open("r") as file:
... updated_config = json.load(file)
...
>>> updated_config["user"]["name"]
'Charlie'
This example demonstrates how you can use the json
module to read, modify, and write JSON data to a file, facilitating tasks such as application configuration management.
Related Resources
Tutorial
Working With JSON Data in Python
In this tutorial, you'll learn how to read and write JSON-encoded data in Python. You'll begin with practical examples that show how to use Python's built-in "json" module and then move on to learn how to serialize and deserialize custom data.
For additional information on related topics, take a look at the following resources:
- Working With JSON in Python (Course)
- Working With JSON Data in Python (Quiz)
By Leodanis Pozo Ramos • Updated July 15, 2025