Python Create JSON File from Dict Example

By Hardik Savani October 30, 2023 Category : Python

Hi Artisan,

If you need to see an example of python create json file from dict. you will learn python write json file from dictionary. you can see how to create json file from list in python. In this article, we will implement a create json file from dict python. Follow the below tutorial step of python create json file from dictionary.

If you want to create a JSON file from the dictionary in python, then I would like to help you step by step on how to write JSON file from disc of dictionaries in python. python has json library to generate JSON file from list using python script. we will use open() and json dump() function to write json file.

So, let's see a simple example with output:

You can use these examples with python3 (Python 3) version.

Example 1:

main.py

import json
      
# Data to be written in dict
dictionary = {
    "id" : 1,
    "name" : "Hardik Savani",
    "email" : "hardik@gmail.com",
    "city" : "Rajkot"
}
   
with open("data.json", "w") as outfile:
    json.dump(dictionary, outfile)
  
print("New data.json file is created from dict")

Output:

After run successfully above example, you will see data.json file saved in your root path and file content will be as the below:

{"id": 1, "name": "Hardik Savani", "email": "hardik@gmail.com", "city": "Rajkot"}

I hope it can help you...

Tags :
Shares