Blog Detail

Lets know JSONPath !!

By:DALEEP SINGH

2021-Jan-24 10:01:00

Recently when one of my colleague was trying to browse through a long list of Kubernetes resources to find resources meeting a specific set of requirements, I asked him, why not use JSONPath to easily browse through it and avoid the hardwork and wasting of time and energy. We together got a JSONPath query done and the work which seemed so tedious was done in no time and we even got the query, which can be reused again, if needed. I did spend some time with him to make him understand the concepts and the way JSONPath works.

So this is my attempt to share the information and also to make it easy for everyone to understand and start using it.

We use multiple formats to write and represent our data, some use YAML, some JSON. All the formats have their respective ways of traversing the data structure and define hierarchy. YAML uses indentation of whitespace to denote structure, JSON ( JavaScript Object Notation ) on other hand uses braces to represent structure.

I have shown a small example with same data in YAML and JSON for an easy comparison.

What is JSONPath?

JSONPath is a query language for JSON provides ways to traverse through a JSON document and allow us to identify subset of the given information quickly and easily.

To demonstrate the use, I thought to take some real-world example which can give us enough understanding to apply those further when we move to K8S resources and objects.

I have prepared a data structure for a class which has two Sections and with marks for some students scored in terms exams during the session. I have shared the data in YAML as well as JSON format so that you can also try JSONPATH with same data on your own a well. ( There are lot of YAML to JSON and vice-versa online converters available. I used https://onlineyamltools.com/convert-yaml-to-json to convert the YAML data to JSON format.). Since I wanted to show the result of my queries, I used a online JSON Evaluator ( https://jsonpath.com/ ) to run the JSONPath queries and show the result. You can also run the commands using the sample data provided at <GITPATH>.

As you can see from the above image, that JSON data is input on the left pane and since, no JSONPath query has been given, no result is displayed on the right pane.

However, you should be able to see that our JSON sample data has “{” starting the data format, which means that our JSON data structure has a hierarchical structure which starts from this “{” and it is called the Root element and in a JSONPath query is represented by “$”. Any JSON document with a Dictionary data type will start with a “$”.  Now once “$” is added as JSONPath query object, entire JSON data is shown in the result.

Let us try to run some basic queries to get a better understanding of JSONPath.

JSONPath Query: $.Class-IX

The result now will include complete data which will include Section-D as well as Section-C from the data-set. You would observe that any output of JSONPath query is enclosed in a pair of Square brackets “[ ]”. This is JSONPath list, which needs to be referred with the Index elements of the list.

The previous query can also be written now as:

JSONPath Query: $.Class-IX[*]

I have put “*” ( wildcard character), as we want to get all Index from the result. However, if you want the first list element, you would refer to index 0 ( $.Class-IX[0]) , for second element as index 1 ( $.Class-IX[1]) and so on.

The above image shows the query run for Index 1 which shows the second element of our JSON data, which is Section-C.

Now lets try to display the result of Finals for Section-C. Section-C is the second element of our JSON dataset and under Section-C, Finals is the 4th element, which when converted to list will be represented by $.Class-IX[1].[3]

JSONPath Query: $.Class-IX[1].[3]

Now let us try to enhance the query for more specific requirement, which is to get all scores for student Ankit Khanna.

JSONPath Query: $.Class-IX[*].[*].[0].Ankit_Khanna

And if you wish to see only for particular subject, append that after a “.” to the query, for example:

$.Class-IX[*].[*].[0].Ankit_Khanna.English 

$.Class-IX[1].[3].[*].English

The best to form JSONPath queries is always to go step-by-step and filtering the data till you reach to your desired data-set. I am sure the above examples would have helped you to understand the flow of JSONPath queries and how to use JSONPath to easily traverse through the JSON data. This will help us to apply the same when we move on to more advanced concepts like applying conditions to filter the data and applying those to kubectl/oc commands.

 

Happy Reading!!