As a developer working on a project for a US-based company, I recently encountered a situation where I needed to store a dictionary containing employee data in a JSON format for easy data exchange with other systems. In this tutorial, I will explain how to save a Python dictionary as a JSON file. In this article, I’ll walk you through the process step-by-step, providing detailed examples along the way.
Understand JSON and Python Dictionaries in Python
Before getting into the solution, let’s briefly discuss what JSON and Python dictionaries are:
- 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 consists of key-value pairs and is commonly used for transmitting data between a server and a web application.
- Python dictionaries are built-in data structures that store data as key-value pairs. They are mutable, unordered, and allow fast lookups using keys.
Read How to Write a Dictionary to a File in Python?
Convert Dictionary to JSON in Python
To convert a Python dictionary to JSON, we can use the json module in Python. The json module provides two methods for this purpose: json.dump() and json.dumps().
json.dump()is used to write the JSON representation of a Python object (e.g., a dictionary) to a file.json.dumps()is used to convert a Python object to a JSON-formatted string.
Let’s consider an example where we have a dictionary containing employee data:
employee_data = {
"name": "John Doe",
"age": 35,
"city": "New York",
"country": "USA",
"skills": ["Python", "Java", "C++"]
}Check out How to Download Files from URLs Using Python?
1. Use json.dump()
To save the Python dictionary as a JSON file using json.dump(), follow these steps:
- Open a file in write mode using the
open()function. - Use
json.dump()to write the Python dictionary to the file. - Close the file.
Here’s an example:
import json
with open("employee.json", "w") as file:
json.dump(employee_data, file)In this code snippet, we open a file named “employee.json” in write mode using the with statement. We then use json.dump() to write the employee_data dictionary to the file. The resulting JSON file will look like this:
{"name": "John Doe", "age": 35, "city": "New York", "country": "USA", "skills": ["Python", "Java", "C++"]}You can see the output in the screenshot below.

Read How to Copy Files to Another Directory in Python?
2. Use json.dumps()
If you need to convert the Python dictionary to a JSON-formatted string instead of saving it directly to a file, you can use json.dumps(). Here’s an example:
import json
json_string = json.dumps(employee_data)
print(json_string)The json.dumps() method converts the employee_data dictionary to a JSON-formatted string, which is then stored in the json_string variable. The output will be:
{"name": "John Doe", "age": 35, "city": "New York", "country": "USA", "skills": ["Python", "Java", "C++"]}You can see the output in the screenshot below.

You can also write this JSON string to a file using the write() method:
with open("employee.json", "w") as file:
file.write(json_string)Output:
{"name": "John Doe", "age": 35, "city": "New York", "country": "USA", "skills": ["Python", "Java", "C++"]}You can see the output in the screenshot below.

Check out How to Open a File in Python?
Best Practices and Tips
Here are some best practices and tips to keep in mind when working with JSON and Python dictionaries:
- Use meaningful and descriptive key names in your dictionaries to improve readability and maintainability.
- Ensure that the keys in your dictionary are strings, as JSON requires keys to be strings.
- If you have complex objects in your dictionary (e.g., custom class instances), you may need to define custom encoders or use the
defaultparameter injson.dump()orjson.dumps()to handle the serialization properly. - When dealing with large datasets, consider using the
indentparameter injson.dump()orjson.dumps()to format the JSON output for better readability. - Always close the file after writing to it to ensure that the data is properly saved and to free up system resources.
Read How to Check if a File Exists in Python?
Conclusion
In this tutorial, I explained how to save a Python dictionary as a JSON file. I discussed JSON and Python dictionaries, and how to convert a dictionary to JSON, I discussed two important methods such as json.dump() and json.dumps() and provided examples of how to use them. I also gave the best practices and tips.
You may read:
- How to Delete a File if it Exists in Python?
- How to Write to a File Without Newline in Python?
- How to Print the Contents of a File in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.