In this article we will show you the solution of create json file python, the full form of JSON is javascript object notation. It is a file that we can easily readable and manageable. We can also write in JSON files.
We can use json. dump() to convert the JSON data into a file. JSON data is nothing but it is a collection of key-value pairs.json data will be written in curly braces.
If there are multiple data, we also create a list to store the data in key-value pairs or created a nested dictionary to write the data in JSON format.
Step By Step Guide On Create JSON File Python :-
Using json.dump() method
#single json data import json a={ "Name":"Varun", "Age": "20", "City": "Lucknow", } #where a is the record of json data b="talkerscode.json" #talkerscode.json is the file where json data is stored with open(b, "w") as myfile: json.dump(a,myfile) #write json data into file.
- Firstly we can import json.
- We initialize a variable “a” that represents the json data in the key-value pairs that represent Name, Age, and City.
- After that we assign a variable b that represents the json file name.
- After that we can use with keyword to open the file in write mode.
- Then we use json.dump() method to convert json-data into file.So we see after running the code the file talkerscode.json is created and we easily access the json-data in terms of Name, Age and City.
#multiple json data import json record = { "record1": { "Name": "Tina", "Age": "20", "City": "Mumbai" }, "record2": { "Name": "Abhinav", "Age": "21", "City": "Delhi" }, "record3": { "Name": "Pooja", "Age": "22", "City": "Pune" }, "record4": { "Name": "Virat", "Age": "24", "City": "Agra" } } b = "talkerscode.json" with open(b, "w") as myfile: json.dump(record, myfile)
- Firstly we can import json.
- We initialize a variable “record” that represents the json data in the key-value pairs that represent record1, record2, and record3.
- Within record1, record2, record3 and record4, we can assign different names,age and city.
- We assign a variable b that represents the json file name.
- After that we can use with keyword to open the file in write mode.
- Then we use json.dump() method to convert json-data into a file. So we see after running the code the file talkerscode.json is created and we easily access the json-data in terms of Name, Age, and City.
import json a=[ {"Name": "Piyush" , "Age":"20", "City": "Kanpur"}, {"Name": "Priyal", "Age": "21", "City": "Barielly"}, {"Name": "Manav", "Age": "22", "City": "Lucknow"}, {"Name": "priyanka", "Age": "23", "City": "Meerut"} ] b="talkerscode.json" with open(b ,"w") as myfile: json.dump(a, myfile)
- Firstly we can import json.
- We assign a variable “a” that passes a json data in the list.
- Within the list, we assign different names, ages, and cities.
- We assign a variable b that represents the json file name.
- After that we can use with keyword to open the file in write mode.
- Then we use json.dump() method to convert json-data into a file.So we see after running the code the file talkerscode.json is created and we easily access the json-data in terms of Name, Age, and City.
Conclusion :-
In conclusion, we successfully learned how to create json file. We used json.dump() method to convert the json data into a file.
And also learned various code examples. You can choose any one of the mentioned code examples that suits you.
I hope this article on create json file python helps you and the steps and method mentioned above are easy to follow and implement.