How To Parse JSON File Content Using Python
- Last Updated on: October 5, 2018
- By: scriptcrunch
Scenario: Consider you have to do the following using python.
- Read a JSON file from a path and parse it.
- Get a JSON from a remote URL (API call etc )and parse it.
This article covers both the above scenarios.
Example JSON:
Following simple JSON is used as an example for this tutorial.
{ "name" : "test", "ip" : "198.168.23.45", "country" : "United States", "project" : "Data Analytics", "website" : "scriptscrunch.com" }
Read a JSON file from a path and parse it
In this scenario, you have a JSON file in some location in your system and you want to parse it.
You need to have the JSON module to be imported for parsing JSON.
The sample code is given below.
import json json = json.loads(open('/path/to/file.json').read()) value = json['key'] print json['value']
You need to replace /path/to/file.json with the relative path of the JSON file.
json = json.loads(open('/path/to/file.json').read())
To get a value from the JSON file, all you need to use the key with json keyword.
value = json['key']
[alert-success]
Recommended Courses
The Python Bible™ | Everything You Need to Program in Python
Complete Python Bootcamp: Go from zero to hero in Python 3
[/alert-success]
Get a JSON from a remote URL and parse it
In this scenario, you have a JSON response from a remote file and you want to parse it.
You need to have json and urllib2 modules for this.
Here is the sample code to do this.
import urllib2 import json req = urllib2.Request("http://jsonplaceholder.typicode.com/todos/1") opener = urllib2.build_opener() f = opener.open(req) json = json.loads(f.read()) print json ip = json['id'] print json['id']
Loop Through JSON Objects
You can also loop through all the JSON objects. You just need to add the following for loop to your code.
json object will hold the actual JSON content.
# print the keys and values for key in json: value = json[key] print("The key and value are ({}) = ({})".format(key, value))
Full code will look like the following.
import json json = json.loads(open('/path/to/file.json').read()) value = json['key'] print json['value'] # print the keys and values for key in json: value = json[key] print("The key and value are ({}) = ({})".format(key, value))
Other Interesting Blogs
CKA Exam Study Guide: Certified Kubernetes Administrator
Passing the cloud-native Certified Kubernetes Administrator (CKA) exam is not a cakewalk. To pass the CKA exam, you should have enough hands-on
Linux Foundation Coupon for 2021
Hi Techies, I wanted to let you know about a pretty sweet deal with the Linux Foundation Coupon that is running now.
[80% OFF] Linux Foundation LFCA,LFCS & LFCE Exam Voucher Codes
Linux Foundation has announced an 80% discount on its Linux certification programs Linux Foundation Certified IT Associate (LFCA) and Linux Foundation Certified
2 thoughts on “How To Parse JSON File Content Using Python”
Hi,
If you want to add all json files to single file how can you do?
and insert in dynamo db.
Hi Rosh,
Please check this thread for JSON concatination..it might help.https://stackoverflow.com/questions/52879192/merge-multiple-json-files-more-than-two/52879638