How to Create JSON File from Dictionary in Python?

05-Jan-2023

.

Admin

How to Create JSON File from Dictionary in Python?

Hi Dev,

In this example, you will learn how to create json file from dictionary. I’m going to show you about python create json file from dictionary. This article goes in detailed on create json file from dict python. let’s discuss about how to create json file from list in python.

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.

Example :


main.py

import json

# Data to be written in dict

dictionary = {

"id" : 1,

"name" : "Piyush Patel",

"email" : "piyush@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:

{"id": 1, "name": "Piyush Patel", "email": "piyush@gmail.com", "city": "Rajkot"}

#Python