All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Create JSON File Python

Last Updated : Mar 27, 2024

Create JSON File Python

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.
  1. Firstly we can import json.
  2. We initialize a variable “a” that represents the json data in the key-value pairs that represent Name, Age, and City.
  3. After that we assign a variable b that represents the json file name.
  4. After that we can use with keyword to open the file in write mode.
  5. 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)
  1. Firstly we can import json.
  2. We initialize a variable “record” that represents the json data in the key-value pairs that represent record1, record2, and record3.
  3. Within record1, record2, record3 and record4, we can assign different names,age and city.
  4. We assign a variable b that represents the json file name.
  5. After that we can use with keyword to open the file in write mode.
  6. 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)
  1. Firstly we can import json.
  2. We assign a variable “a” that passes a json data in the list.
  3. Within the list, we assign different names, ages, and cities.
  4. We assign a variable b that represents the json file name.
  5. After that we can use with keyword to open the file in write mode.
  6. 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.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪