How to Create JSON File from Dict in Python?

Published On: 18/01/2023 | Category: Python

Hi Dev,

Today our leading topic is python write json file from dictionary. I explained simply step by step how to create json file from dictionary in python. you will learn python create json file from dict. I’m going to show you about create json file from dict python.

So, if you want to create json file from dict in python then i would like to describe python has json library to generate JSON file using python script. we will use open() and json dump() function to write json file.

So let's see bellow example:

import json
# Data to be written in dict
dictionary = {
    "id" : 1,
    "name" : "Bhavesh Sonagra",
    "email" : "[email protected]",
    "city" : "Rajkot"
with open("data.json", "w") as outfile:
    json.dump(dictionary, outfile)
print("New data.json file is created from dict")

After running example successfully, you will see a data.json file saved in your root path, with the following content:

{"id": 1, "name": "Bhavesh Sonagra", "email": "[email protected]", "city": "Rajkot"}

It will help you....