With Python, you can read from and write data to a variety of data sources including CSV, TSV, JSON and databases. Several libraries are available in Python for data reading and writing tasks, including the CSV module and the sqlite3 module for databases. In this tutorial, we’ll show you how to read and write JSON files with Python using Python’s json module. At the end, we’ll explain how the pandas library makes it easier to read JSON files into tabular format.
What is a JSON File
JSON stands for JavaScript Object Notation which is one of the most widely used formats for data transfer and serialized information exchange between client server applications. JSON data is human-readable which makes it one of the most popular formats for transferring data over REST APIs.
Writing JSON Files with Python
JSON files store data in a specific format. One way to make JSON compliant data in Python is to create nested dictionaries where you have an outer dictionary containing multiple items in the form of key-value pairs. The key for each pair is a data attribute. The corresponding value for a key is another dictionary that contains records for the attribute. For instance, look at the
departments_dic = {
"Department_Name":{"0":"Development","1":"QA","2":"HR","3":"Finance", "4":"Configuration"},
"Department_Manager":{"0":"Sally","1":"Joana","2":"Nick","3":"Robert", "5": "Sara"},
"Department_Type":{"0":"Tech","1":"Tech","2":"Admin","3":"Admin", "4":"Tech"},
"Total_Employees" :{"0":25,"1":15,"2":5,"3":5, "4":10}
}
Each item in the
Another way to create a Python object that can be written to a JSON file is to initialize a list of dictionaries. Items within dictionaries correspond to a single record. The key for each item is the attribute name while the corresponding value represent the value for that attribute. For example, look at the
departments_list = [
{"Department_Name":"Development", "Department_Manager":"Sally", "Department_Type":"Tech", "Total_Employees":25},
{"Department_Name":"QA", "Department_Manager":"Joana", "Department_Type":"Tech", "Total_Employees":15},
{"Department_Name":"HR", "Department_Manager":"Nick", "Department_Type":"Admin", "Total_Employees":5},
{"Department_Name":"Finance", "Department_Manager":"Robert", "Department_Type":"Admin", "Total_Employees":5},
{"Department_Name":"Configuration", "Department_Manager":"Sara", "Department_Type":"Tech", "Total_Employees":10}
]
We now have two objects that can be written to JSON files: dump()
function of the dump()
method. The following Python script writes two JSON files using
import json
with open('G:/departments_dic.json', 'w') as jf:
json.dump(departments_dic, jf)
with open('G:/departments_list.json', 'w') as jf:
json.dump(departments_list, jf)
Code More, Distract Less: Support Our Ad-Free Site
You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.
Reading JSON Files with Python JSON Module
Python’s open()
method. The object returned by the open()
method is then passed to the load()
method of the load()
method returns a dictionary which is then iterated using a foreach
loop and all the items of the dictionary are displayed on the console.
import json
json_file = open('G:/departments_dic.json',)
data = json.load(json_file)
for k,v in data.items():
print (k,v)
Here is the output of the script above:
Output:
Department_Name {'0': 'Development', '1': 'QA', '2': 'HR', '3': 'Finance', '4': 'Configuration'}
Department_Manager {'0': 'Sally', '1': 'Joana', '2': 'Nick', '3': 'Robert', '5': 'Sara'}
Department_Type {'0': 'Tech', '1': 'Tech', '2': 'Admin', '3': 'Admin', '4': 'Tech'}
Total_Employees {'0': 25, '1': 15, '2': 5, '3': 5, '4': 10}
In the same way, the following script reads the
import json
json_file = open('G:/departments_list.json',)
data = json.load(json_file)
for i in data:
print (i)
Output:
{'Department_Name': 'Development', 'Department_Manager': 'Sally', 'Department_Type': 'Tech', 'Total_Employees': 25}
{'Department_Name': 'QA', 'Department_Manager': 'Joana', 'Department_Type': 'Tech', 'Total_Employees': 15}
{'Department_Name': 'HR', 'Department_Manager': 'Nick', 'Department_Type': 'Admin', 'Total_Employees': 5}
{'Department_Name': 'Finance', 'Department_Manager': 'Robert', 'Department_Type': 'Admin', 'Total_Employees': 5}
{'Department_Name': 'Configuration', 'Department_Manager': 'Sara', 'Department_Type': 'Tech', 'Total_Employees': 10}
Reading JSON Files with Pandas
Though you can use the read_json()
function from the
import pandas as pd
dep_dic = pd.read_json('G:/departments_dic.json')
dep_dic.head()
The read_json()
function returns a pandas dataframe. The output below shows the JSON data in tabular format from our pandas dataframe.
Output:
In the same way, you can convert the
import pandas as pd
dep_list = pd.read_json('G:/departments_list.json')
dep_list.head()
Output:
Converting Pandas Dataframe to JSON
Finally, you can convert a pandas dataframe to a JSON file using the to_json()
method, as shown below.
import pandas as pd
dep_list = pd.read_json('G:/departments_list.json')
dep_list.to_json('G:/dep_list2.json', orient='records', lines=True)
If you open the
Output:
Code More, Distract Less: Support Our Ad-Free Site
You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.