JSONPath is an expression language used to parse JSON data in Python. JSONPath is similar to XPath in XML, where we print XML data.
JSONPath provides a simpler syntax for querying JSON data and obtaining the required value in Python. Using JSONPath is a more efficient way to analyze and query JSON data because there is no need to retrieve the entire JSON data. This method is more memory-optimized compared to any JSON query method.
JSONPath Library in Python
There are many JSONPath libraries for Python, and the best known is the jsonpath-ng library. It is written in native Python and supports Python 2 and Python 3 versions.
Jsonpath-ng is the definitive implementation of JSONPath for Python, which aims to adhere to the standard, including arithmetic and binary comparison operators, as defined in the original JSONPath design.
These packages integrate jsonpath-rw and jsonpath-rw-ext and provide many AST API enhancements, such as the ability to update or delete tree nodes.
Installing jsonpath-ng Module
To install jsonpath-ng library, use the below pip install command.
pip install --upgrade jsonpath-ng
The above command will install the latest version of the jsonpath-ng library on your machine. Once installed, you can import in the Python IDE using the below code.
import jsonpath_ng
Jsonpath operators:
Below are the list of operators you can use for getting json data values.
Syntax | Meaning |
---|---|
jsonpath1 . jsonpath2 | All nodes matched by jsonpath2 starting at any node matching jsonpath1 |
jsonpath [ whatever ] | Same as jsonpath.whatever |
jsonpath1 .. jsonpath2 | All nodes matched by jsonpath2 that descend from any node matching jsonpath1 |
jsonpath1 where jsonpath2 | Any nodes matching jsonpath1 with a child matching jsonpath2 |
jsonpath1 | jsonpath2 | Any nodes matching the union of jsonpath1 and jsonpath2 |
Parsing a Simple JSON Data using JSONPath
A Simple example of parsing the JSON and fetching the JSON value using the attribute key.
# Program to parse JSON data in Python import json from jsonpath_ng import jsonpath, parse employee_data = '{"id":1, "first_name":"Chandler" , "last_name":"Bing"}' json_data = json.loads(employee_data) jsonpath_expr= parse('$.first_name') first_name = jsonpath_expr.find(json_data) print("The First Name of the employee is: ", first_name[0].value)
Output
The First Name of the employee is Chandler
Parsing a Json Array using JSONPath Expression
The JSON key contains the list of values and uses the JSON Path expression. We can parse and query the exact field values of the JSON.
{ "books": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "isbn": "6-246-2356-8", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "isbn": "5-444-34234-8", "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ] }
In the above JSON data, if we need the list of all ISBN of the book, we can use the below code to get the data using JSONPath expression as shown below.
# Program to parse JSON data in Python import json from jsonpath_ng import jsonpath, parse with open("books.json", 'r') as json_file: json_data = json.load(json_file) jsonpath_expression = parse('books[*].isbn') for match in jsonpath_expression.find(json_data): print(f'Books ISBN: {match.value}')
Output
Books ISBN: 6-246-2356-8 Books ISBN: 5-444-34234-8 Books ISBN: 0-553-21311-3 Books ISBN: 0-395-19395-8