Make Selenium Easy

REST Assured Tutorial 59 – How To Create JsonPath For Simple And Nested JSON Array?

Introduction

As a part of the End to End REST Assured Tutorial, in this post, we will learn to write JsonPath for simple and nested JSON arrays.

If you are familiar with Xpath then you can understand JsonPath easily. The only difference is that XPath represents a path to reach a node in an XML document while JsonPath represents a path to reach a node in a JSON document.

Topics we are going to learn in this post

  1. How to write JsonPath for a simple JSON array?
  2. How to write JsonPath for nested JSON arrays?

Prerequisite

As we are using RestAssured which includes JsonPath dependency by default, there is no need to include JsonPath dependency explicitly. I have used the below version of Rest Assured for this post:-

I have covered about JsonPath and writing JsonPath for JSON objects in the below post:-

What Is JsonPath And How To Create It For Simple And Nested JSON Object?

JsonPath for simple JSON array

A JSON Array contains one or more elements separated by a comma. For example-

In the above JSON, the JSON element or node “address” holds an array and consists of two elements or JSON Objects. Both elements in that array are separated by a comma. In any programming language, we can refer to or access elements using its index in an array. Index starts from zero. If I do “address[0]” it will represent the first element in the “address” array. Similarly “address[1]” will represent the second element in the “address” array. The same logic applied while writing JsonPath for it.

Once we get indexed element we can access their children using the dot operator which we have learned in the previous post.

JsonPath as “address[0].type” will return us “permanent” and “address[1].state” will return “MP”.

JsonPath as “address” will give us all elements of the JSON array.

We can also retrieve a specific field value from all elements of an array using getList() method provided by JsonPath.

Example program

Output

JsonPath for nested JSON array

A JSON array inside a JSON array is called nested JSON arrays and writing JsonPath is simple for these. Just we need to use indexes.

Example program

Output

You can download/clone the above sample project from here.

You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

REST Assured Tutorial 59 – How To Create JsonPath For Simple And Nested JSON Array?

by Amod Mahajan time to read: 4 min
0
%d bloggers like this: