This guide explains how the json.dump() and json.dumps in Python operate — a key tool for storing structured data in JSON format. This approach is widely applied when working with configuration files, web scraping results, API responses, and other structured data. The material covers the function’s syntax, its parameters, difference between json.dump vs json.dumps, usage examples, supported data types, as well as best practices for efficient and reliable handling of JSON in Python.
json.dump is a function from the json module used to serialize a Python object into JSON format and directly save it to a file.
The json.dump() function accepts two main arguments — the data and the file object where the JSON output will be written. The general call structure looks as follows:
json.dump(obj, file, **kwargs)
In addition to the main arguments, several optional parameters are available:
These parameters are particularly useful when the readability of the data is important, as well as when preparing files for machine processing.
The Python json dump function is applied for serializing data and immediately saving it to a file in JSON format. It is useful in cases where results of web scraping, application configuration, or collected data need to be stored in a file for further analysis or transfer.
Key advantages include:
The json module is part of Python’s standard library, meaning no third-party installations are required — an import at the beginning of the script is sufficient.
The json.dump() function supports serialization only of objects that can be represented in JSON format. These include lists, dictionaries, strings, integers, floating-point numbers, Boolean values, and None.
An example demonstrating typical usage of json.dump() for saving a dictionary:
import json
data = {
"name": "Helen",
"age": 28,
"is_active": True,
"skills": ["Python", "SQL"]
}
with open("user.json", "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
If an attempt is made to serialize unsupported objects (such as classes or functions), a TypeError will be raised.
Python provides two similar functions for serializing data into JSON format — json.dump() and json.dumps(). Despite the similarity in their names, they serve different purposes:
The difference between json.dump() and writing the output of json.dumps() to a file is illustrated in the examples below:
import json
data = {"key": "value"}
# Using json.dump()
with open("output.json", "w", encoding="utf-8") as f:
json.dump(data, f)
# Using json.dumps()
json_string = json.dumps(data)
print(json_string) # {"key": "value"}
The key distinction between the two is based on the task:
Saving data to a JSON file is one of the most common scenarios in Python. To do this, just create a Python object (for example, a dictionary), open the file in write mode, and pass the object to the json.dump() function.
Writing JSON to a File in Python:
import json
# 1. Data to be stored
user_data = {
"username": "admin",
"active": True,
"roles": ["editor", "moderator"]
}
# 2. Open file in write mode
with open("data.json", "w", encoding="utf-8") as f:
# 3. Write to file
json.dump(user_data, f, ensure_ascii=False, indent=4)
In this example:
When working with JSON in Python, it is advisable to follow a set of practices that simplify data handling and increase code reliability:
Following these principles helps prevent errors and improves scalability, especially in tasks related to collecting and processing large volumes of structured information.
json.dump() is a reliable and efficient tool for saving structured data in JSON format, it enables writing Python objects directly to files while supporting formatting, key sorting, and proper handling of multilingual text. The json.dumps() function complements it by providing string-based serialization for flexible processing. These functions cover a wide range of use cases, from storing configuration files to managing results of web scraping and API responses.
Adhering to best practices ensures readability, correctness, and stability when working with JSON in Python, making these tools an integral part of modern development workflows.
Comments: 0