JSONPath is an expression language is used to parse JSON data in Python. JSONPath is similar to XPath expression language, where we parse to the XML data.
To query JSON data and retrieve the desired value in Python, JSONPath offers a more simpler syntax. Since we don’t have to load the complete JSON data, using JSONPath will be a more effective way to parse and query JSON data. Compared to other JSON query methods, this strategy is more memory-optimized.
JSONPath Library in Python
There are many JSONPath libraries for Python, and One of the most popular library is jsonpath-ng. jsonpath-ng is the most feature-complete. It’s also Supports both Python 2 and Python 3 versions.
The final version of JSONPath for Python, jsonpath-ng, attempts to be standard compliant and includes binary and arithmetic comparison operators.
This library merges jsonpath-rw and jsonpath-rw-ext modules and further enhancements. Such as ability to update or remove nodes.
Installing jsonpath-ng Module
To install the jsonpath-ng module, use the below pip install command.
pip install --upgrade jsonpath-ng
Above you can see install the latest version of the jsonpath-ng library on your machine. After successfully installed you can import in the Python IDE using the below code.
import jsonpath_ng
Parsing a Simple JSON Data using JSONPath
A Simple example to parse the JSON data and get the required attribute value.
import json
from jsonpath_ng import jsonpath, parse
students_data = '{"id":1, "first_name":"Mir" , "last_name":"Soyel"}'
json_data = json.loads(students_data)
jsonpath_expr= parse('$.first_name')
first_name = jsonpath_expr.find(json_data)
print("The First Name of the student is: ", first_name[0].value)
Output
The First Name of the employee is Mir
Using JSONPath in Python
The JSON key may be contain a list of values. To parse the list and obtain the values list, we can use a JSONPath expression. Let’s use a simple json structure with data.
{
"students": [
{
"name": "Michael",
"age": "15",
"class": "10",
"city": New York
},
{
"name": "Emily",
"age": "17",
"class": "12",
"city": Los Angeles
},
{
"name": "Hannah",
"age": "12",
"class": "7",
"city": Chicago
},
{
"name": "Sarah",
"age": "14",
"class": "9",
"city": Houston
}
]
}
Above the JSON data, we need the list of all age of the students. We have to use below the code to get the data using JSONPath expression.
import json
from jsonpath_ng import jsonpath, parse
with open("students.json", 'r') as json_file:
json_data = json.load(json_file)
jsonpath_expression = parse('students[*].name')
for match in jsonpath_expression.find(json_data):
print(f'Students Name: {match.value}')
Output
Students Name: Michael
Students Name: Emily
Students Name: Hannah
Students Name: Sarah
Conclusion
JSONPath provides us a simple method to parse JSON data and extract particular values. When there is a lot of JSON data and only few of the values are of interest, it is quite helpful.
we hope this article has been informative. Thank you for reading. Kindly comment and let us know if you found it helpful.