How to Create a JSON File in Python?
Hi Guys,
In this guide, we are going to learn how to create json file in python. This article will give you a simple example of how to generate json file in python. you'll learn python create json file and write. I would like to show you how to save json file in python. So, let us see in detail an example.
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.
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 # 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:
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" }, { "ID": 2, "Name": "Vimal Kashiyani", "email": "vimal@gmail.com" }, { "ID": 3, "Name": "Harshad Pathak", "email": "harshad@gmail.com" } ]
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Get Column Names from CSV File in Python?
- Python Read CSV File Line by Line Example
- Python Read CSV File Without Header Example
- How to Read a CSV File in Python?
- How to Write CSV File in Python?
- How to Check If a List is Empty or Not in Python?
- Python Read Text File into List Example
- How to Append Text or Lines to a Text File in Python?
- Python Generate Text File from List Example
- How to Capitalize String in Python?
- Python Create Zip Archive from Directory Example
- Python PUT Request with Parameters Example