Create a JSON File in Python Tutorial Example

30-Dec-2022

.

Admin

Create a JSON File in Python Tutorial Example

Hi Dev,

In this short tutorial we will cover an create a json file in python tutorial example. This article goes in detailed on how to write string to json file in python. you will learn how to write data to json file in python. you will learn how to save json file in python. You just need to some step to done python create json file and write.

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

Example :


main.py

import json

# Create List for write data into json file

data = [

{ "ID": 1, "Name": "Hardik Savani", "email": "hardik@gmail.com"},

{ "ID": 2, "Name": "Vimal Kashiyani", "email": "vimal@gmail.com"},

{ "ID": 3, "Name": "Harshad Pathak", "email": "harshad@gmail.com"}

]

# Create Json file with list

with open('data.json', 'w') as f:

json.dump(data, f, indent=2)

print("New data.json file is created from list")

Output:

[

{

"ID": 1,

"Name": "Hardik Savani",

"email": "hardik@gmail.com"

},

{

"ID": 2,

"Name": "Vimal Kashiyani",

"email": "vimal@gmail.com"

},

{

"ID": 3,

"Name": "Harshad Pathak",

"email": "harshad@gmail.com"

}

]

#Python