How to Use json.dump() in Python: A Beginner-Friendly Guide


Justas Vitaitis
Key Takeaways
-
json.dump() writes Python object data into a JSON file.
-
Only simple types like lists, numbers, and Python dictionaries can be saved without extra work.
-
Use indent and sort_keys to make your JSON output easier to read.
In This Article
Learning how to save data is probably one of the most important parts of coding. If you’re working with JSON data or a Python object, you’ll need the right tools.
One of the best ones is the json.dump() function. After reading this guide, you’ll know exactly how to use it, even if you’re a complete beginner. Let’s get to it.
What Is json.dump() in Python?
json.dump() is a method from the JSON module. It lets you write JSON data directly into a JSON file instead of just printing it to stdout with something like print() , you can save it for later.
In most basic terms, it takes a Python object, like a Python dictionary or list, and turns it into an easily accessible and transferable JSON format file. This format follows the rules of JavaScript Object Notation, which is a way to make data easy to share between computers and people.
When and Why to Use json.dump()
You use json.dump() when you want to save JSON data safely. For example, if you build a program that tracks productivity, you may want to store your work in a json file to prevent losing all your data.
The JSON format is simple and easy to understand, which makes it extremely useful for anyone using it. You can even open a JSON document and see your saved information without downloading any special software, like you may need to do with other formats.
In projects like web applications or games, saving all settings as JSON data is a very common and popular practice.
If you’d like to deepen your knowledge about all things JSON, check out our article on reading, writing, and parsing JSON .
What Types of Data Can Be Dumped to JSON in Python?
The JSON module can turn many types of Python objects into JSON data. Here’s what you can save:
- Python dictionaries
- Lists
- Strings
- Numbers
However, you may face issues trying to save some other types of JSON data. If you try to save a Python set or some custom object, you’ll most likely get a TypeError.
The reason is simple – JSON encoders can’t guess how to turn unrecognized types into the JSON format.
If there’s no way for you to change the data into something simple and recognizable, like a list or Python dictionary, you’ll have to make your own JSON encoder to fix it.
Syntax of json.dump()
import json
json.dump(obj, file_object, indent=4, sort_keys=True)
This is the basic json.dump() syntax. Here’s an explanation of each argument:
- obj: The python object you want to save as JSON data.
- file_object: Where you want to write the JSON output.
- indent: Makes the JSON format easy to read and understand.
- sort_keys: Puts the keys in alphabetical order.
For example, if you use indent-4 , your JSON document will look nice and clean as it will transfer indentation spaces to the file. And if you use sort_keys=True , it will arrange your keys alphabetically.
json.dump() vs json.dumps()
While looking at many different functions, you might notice that there’s also a json.dumps() method. To answer shortly, no, they are not the same. So, what’s the difference?
Feature | json.dump() | json.dumps() |
---|---|---|
Purpose | Writes JSON data to a JSON file | Turns a Python object into a JSON string |
Output | Goes to a file | Stays in memory (string) |
Common use | Saving data to disk | Sending data over a network |
In short, you should:
- Use json.dump() when you want to save JSON data to a JSON file.
- Use json.dumps() when you need a JSON string to send or print.
If you’d like to learn more about how JSON compares to other formats, here’s an article about JSON and CSV .
Step-by-Step Guide: Saving JSON to a File
import json
data = {
"name": "Tyler",
"age": 27,
"city": "New York"
}
with open('data.json', 'w', encoding='utf-8') as f:
json.dump(data, f, indent=4)
Here’s a simple example with each step explained:
- First, you import json . No need to install it as it’s a default library.
- Then, you create a Python dictionary and store it in data .
- Next, you open a file called data.json in write mode.
- Finally, you use json.dump() to save the Python object as JSON data.
If you run this code twice, it will overwrite the JSON file each time unless you change the file name. Also, always make sure to use encoding=’utf-8’ to handle any irregular letters or symbols. If needed, you can use different encodings, but utf-8 covers 99% of use cases.
Sometimes, especially if you want someone else to check it, you’ll want to make sure the JSON output is easy to read. To do that, you should use the indent setting like we explained before. It makes the JSON document look neat.
If JavaScript piques your interest, you may also want to read about how JavaScript object notation parsing works .
Best Practices of Working with JSON in Python
Working with JSON data in Python can be easy if you follow a few rules.
1. Always Use the JSON Module When You Need to Handle JSON Data
It’s built into Python and knows exactly how to turn a Python object into JavaScript Object Notation. You should always avoid trying to make a JSON string by hand. That’s just asking for trouble and unnecessary errors.
2. Make it Readable
When saving a Python dictionary or list, always use json.dump Python with indent=4. This will make your JSON file readable. It’s a small thing, but it makes a big difference when you open the JSON file later or give it to someone else.
3. Stick to Simple Types
Always make sure your Python object only has simple types inside it. Keep it to JSON objects like dictionaries, lists, strings, and numbers. If you try to save some intricate objects like custom classes or sets, you’ll most likely mess up the JSON format unrecognizably.
4. Manage Special Letters and Symbols
When writing a JSON string to a file, open the file with encoding=’utf-8’. This way, if you have special letters that aren’t standard in the English language, your JSON data won’t get scrambled. Otherwise, the whole file may turn out to be chaotic.
5. Sort Your Keys
Sort your keys when saving a Python dictionary. Use sort_key=True with json.dump() so your JSON file stays neat and easy to compare later. When you use sorted keys, you can even spot bugs faster.
If you need to load JSON data from another program, stick to the basics: strings must use double quotes, not single quotes, and only use simple types.
By following these rules, your JSON objects should always stay clean, your Python object should always save correctly, and your JSON file will always be ready to share or read later.
Conclusion
Using json.dump() is easy once you understand the basics. It helps you save JSON data safely and quickly. Using this function from the JSON module makes saving a Python object into a neat JSON file easier than you may think. Just make sure to stick to your Python dictionary.
Also, JSON format is meant to be simple and universal, so sticking to clean JSON objects is important. Always double-check that your Python object can cleanly become JSON data, otherwise you’ll get errors.
Don’t forget to always use import json at the top of your files. It’s a small thing, but crucial. Without it, you won’t be able to access the tools in the JSON module. Once you load the module, you can easily turn a Python dictionary into a nice JSON string and save it to a JSON file.

Author
Justas Vitaitis
Senior Software Engineer
Justas is a Senior Software Engineer with over a decade of proven expertise. He currently holds a crucial role in IPRoyal’s development team, regularly demonstrating his profound expertise in the Go programming language, contributing significantly to the company’s technological evolution. Justas is pivotal in maintaining our proxy network, serving as the authority on all aspects of proxies. Beyond coding, Justas is a passionate travel enthusiast and automotive aficionado, seamlessly blending his tech finesse with a passion for exploration.
Learn More About Justas Vitaitis