Python

JSONPath in Python

Before we begin to learn how to use the JSONPath in Python, let us briefly introduce JSONPath. JSONPath is an expression language. It is used to parse a JSON string or construct a JavaScript object described by the string in Python. The functionality of JSONPath in Python is similar to the XPath in XML. XPath is used to parse the data in XML; similarly, JSONPath is used to parse the data in Python. The idea is to use an efficient way to parse the JSON data without loading the entire JSON data or going through the entire JSON data in Python and get the desired result. JSONPath is a memory-optimized approach compared to any other JSON query. It also provides a way to parse JSON data in Python with a few lines of code. JSONPath has various libraries. In this article, we will learn about the JSONPath libraries and how to use those libraries with the help of examples. So let us begin the learning process.

JSONPath Libraries

Though there are many JSONPath libraries, the most popular ones are jsonpath-rw, jsonpath-rw-ext, and jsonpath-ng. The jsonpath is the port of the Perl, and it is the JS version of JSONPath. The jsonpath-rw is a complete implementation of the JSONPath library. The expressions in jsonpath-rw are first-class objects as they provide language extension. These expressions are easy to transform, extend, parse, and analyze.

It also provides some additional extensions which extend the functionalities of JSONPath. The jsonpath-rw-ext extends the capabilities of jsonpath-rw while adding multiple extensions, i.e., filter, arithmetic, len, etc. The jsonpath-ng library is written in native python language and the final implementation of the JSONPath. It supports both versions of Python; python 2 and python 3. It is intended for standard complaints that include both binary and arithmetic comparison operators defined in the original JSONPath.

Installing JSONPath Libraries

The jsonpath-rw, jsonpath-rw-ext, and jsonpath-ng libraries can be simply installed using the pip install command. Below is a step-by-step guide to installing the JSONPath libraries, and you can learn how to use these libraries with the help of examples.

Example 1:

Our first example is about the jsonpath-rw library. The jsonpath-rw library offers considerably significant and robust extended implementation of JSONPath in Python. It is well-matched with python 2 and Python 3 versions of Python and their extensions, i.e., python 2.7, python 3.4, python 3.5, python 3.6, python 3.7, and pypy pypy3. To install the jsonpath-rw, use the listed command.

A picture containing text Description automatically generated

When you execute the pip install command, the following output will indicate the successful installation of jsonpath-rw.

A picture containing text Description automatically generated

Once the library is installed, you can use it to parse JSON data in Python to produce any desired result. See the following example of jsonpath-rw for your understanding:

import json
from jsonpath_rw import jsonpath, parse

Students_data = '{"SrNo":1, "Stu_Name":"Robbin" , "Father_name":"Royel"}'
JsonData = json.loads(Students_data)

Json_Path_Expr= parse('$.Stu_Name')
Stu_Name = Json_Path_Expr.find(JsonData)

print("The Name of the student is: ", Stu_Name[0].value)

Text Description automatically generated

The following output you will get after using the jsonpath-rw library.

Example 2:

Now we will be discussing the jsonpath-rw-ext library. To install the jsonpath-rw-ext library, the same pip install command is used, have a look:

After the successful installation of the jsonpath-rw-ext library, you will get the following output:

Text Description automatically generated

Let us see an example of jsonpath-rw-ext to learn how it will help to parse the JSON data in python.

import jsonpath_rw_ext as jpExt

print (jpExt.match('$.que[*]', {'que'['tex', 'bar'], 'next''quetex'}))

When you execute the above code in any Python interpreter, you will get the following output:

Example 3:

Our last example is about the JSONPath NG library. Again, to install the JSONPath NG library, you need to use the listed command.

After successfully installing the jsonpath-ng library, you will get to see the following output.

Text Description automatically generated

To upgrade the jsonpath-ng library, you need to use the listed command. Here is the syntax of –upgrade python command.

The command given above will install the latest version of the jsonpath-ng library. However, if your system has already installed the latest version, then you’ll get the following output:

Here is a simple example of the jsonpath-ng library to help you learn how to parse JSON data in Python using the JSONPath-ng library and get the desired result.

import json
from jsonpath_ng import jsonpath, parse

booksData = '{"Category":"Fiction", "Author":"Herman Melville" , "Title":"Sayings of the Century"}'
Data = json.loads(booksData)
ParseData= parse('$.Category')
Category = ParseData.find(Data)
ParseData1= parse('$.Author')
Author = ParseData1.find(Data)
ParseData2= parse('$.Title')
Title = ParseData2.find(Data)

print("The Category of the book is: ", Category[0].value)
print("The Author of the book is: ", Author[0].value)
print("The Name of the book is: ", Title[0].value)

Text Description automatically generated

Execute the code given above in any python interpreter, and you will get the subsequent output.

Text Description automatically generated

Conclusion

In this editorial, we have learned about the JSONPath in Python. Let’s have a quick recap of the article. The JSONPath in Python provides a simple, easy, and quick way to parse the JSON data in Python to get the desired result without going through the whole JSON data in Python. It helps to extract the specific value from a huge amount of data while saving time and memory. It comes in handy when we are only interested in some specific values in a huge amount of data. We have seen three JSONPath libraries, jsonpath-rw, jsonpath-rw-ext, and jsonpath-ng libraries, and with the help of examples, we have learned how to install and use these libraries to parse the JSON data in Python. These libraries are python builtin JSONPath libraries which come in the python package.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content