Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 03/21/2025 By admin

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?

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:-

  io.rest-assured rest-assured 4.3.3 test

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?

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

{
  "firstName": "Amod",
  "lastName": "Mahajan",
  "address": [
    {
      "type": "permanent",
      "city": "Bengaluru",
      "state": "KA"
    },
    {
      "type": "temp",
      "city": "Bhopal",
      "state": "MP"
    }
  ]
}

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.

package JsonPathForJsonArray; import java.util.List;
import java.util.Map; import io.restassured.path.json.JsonPath; public class SimpleJsonArray { public static void main(String[] args) { String jsonArrayString = "{\r\n" + " \"firstName\": \"Amod\",\r\n" + " \"lastName\": \"Mahajan\",\r\n" + " \"address\": [\r\n" + " {\r\n" + " \"type\": \"permanent\",\r\n" + " \"city\": \"Bengaluru\",\r\n" + " \"state\": \"KA\"\r\n" + " },\r\n" + " {\r\n" + " \"type\": \"temp\",\r\n" + " \"city\": \"Bhopal\",\r\n" + " \"state\": \"MP\"\r\n" + " }\r\n" + " ]\r\n" + "}"; //Get JsonPath instance of above JSON string JsonPath jsonPath = JsonPath.from(jsonArrayString); // Since address holds a JSON array we can get particular indexed element using index String addressType1 = jsonPath.getString("address[0].type"); System.out.println("Address type is : "+addressType1); String addressType2 = jsonPath.getString("address[1].type"); System.out.println("Another address type is : "+addressType2); // We can get address types as a list as well List allAddressTypes = jsonPath.getList("address.type"); System.out.println(allAddressTypes); // We can get complete address array as List // Since it holds Json objects which can be a Map List> allAddress = jsonPath.getList("address"); for(Map address : allAddress) { System.out.println(address); } } } 
Address type is : permanent
Another address type is : temp
[permanent, temp]
{type=permanent, city=Bengaluru, state=KA}
{type=temp, city=Bhopal, state=MP}

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.

package JsonPathForJsonArray; import java.util.List;
import java.util.Map; import io.restassured.path.json.JsonPath; public class NestedJsonArray { public static void main(String[] args) { String jsonArrayString = "{\r\n" + " \"firstName\": \"Amod\",\r\n" + " \"lastName\": \"Mahajan\",\r\n" + " \"address\": [\r\n" + " [\r\n" + " {\r\n" + " \"type\": \"permanent\",\r\n" + " \"city\": \"Bengaluru\",\r\n" + " \"state\": \"KA\"\r\n" + " },\r\n" + " {\r\n" + " \"type\": \"temp\",\r\n" + " \"city\": \"Bhopal\",\r\n" + " \"state\": \"MP\"\r\n" + " }\r\n" + " ],\r\n" + " [\r\n" + " {\r\n" + " \"type\": \"communication\",\r\n" + " \"city\": \"Delhi\",\r\n" + " \"state\": \"DL\"\r\n" + " },\r\n" + " {\r\n" + " \"type\": \"old\",\r\n" + " \"city\": \"Kanpur\",\r\n" + " \"state\": \"UP\"\r\n" + " }\r\n" + " ]\r\n" + " ]\r\n" + "}"; //Get JsonPath instance of above JSON string JsonPath jsonPath = JsonPath.from(jsonArrayString); // Since address holds nested JSON arrays we can get particular indexed element using index // followed by another index String addressType1 = jsonPath.getString("address[0][0].type"); System.out.println("Address type is : "+addressType1); String addressType2 = jsonPath.getString("address[0][1].type"); System.out.println("Second address type is : "+addressType2); String addressType3 = jsonPath.getString("address[1][0].type"); System.out.println("Third type is : "+addressType3); String addressType4 = jsonPath.getString("address[1][1].type"); System.out.println("Fourth address type is : "+addressType4); // We can get address types from first array of address List allAddressTypesOfFirstElementOfArray = jsonPath.getList("address[0].type"); System.out.println(allAddressTypesOfFirstElementOfArray); // We can get address types from second array of address List allAddressTypesOfSecondElementOfArray = jsonPath.getList("address[1].type"); System.out.println(allAddressTypesOfSecondElementOfArray); // We can get address types from all elements of array of address List allAddressTypes = jsonPath.getList("address.type"); System.out.println(allAddressTypes); }
}
Address type is : permanent
Second address type is : temp
Third type is : communication
Fourth address type is : old
[permanent, temp]
[communication, old]
[[permanent, temp], [communication, old]]

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

Uncategorized

Post navigation

Previous Post: Log4j2 Tutorial 4 – Print Logs In External File Using XML Configuration File of Log4J2
Next Post: TestNG Tutorials 9: Internal Logic Of Generation Of TestNG.xml

Related Posts

components – Make Selenium Easy Uncategorized
Postman Tutorial Part 3 – Sending First GET Request in Postman – Make Selenium Easy Uncategorized
Linux Basic Commands – date Uncategorized
image – Make Selenium Easy Uncategorized
Amod Mahajan – Page 2 – Make Selenium Easy Uncategorized
July 29, 2017 – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark