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
- How to write JsonPath for a simple JSON array?
- 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:-
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?
JsonPath for simple JSON array
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.
Example program
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 ListallAddressTypes = 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
Output
Address type is : permanent Another address type is : temp [permanent, temp] {type=permanent, city=Bengaluru, state=KA} {type=temp, city=Bhopal, state=MP}
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
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 ListallAddressTypesOfFirstElementOfArray = 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); } }
Output
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
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.