Introduction
As a part of the End to End REST Assured Tutorial, in this post, we will learn to learn more JsonPath syntax and expressions.
We have already covered the introduction of JsonPath and creating JsonPath for simple and nested JsonObjects and JsonArrays. You can refer to those posts here:-
What Is JsonPath And How To Create It For Simple And Nested JSON Object?
How To Create JsonPath For Simple And Nested JSON Array?
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
Basic concepts of JsonPath supported by Rest Assured
JsonPath in Rest Assured uses Groovy’s GPath notation and is not to be confused with Jayway’s JsonPath syntax. In simple words there are many JsonPath syntaxes you will see around but all will not be supported by RestAssured JsonPath. In this post, we will see all supported JsonPath syntaxes by Rest Assured.
The root node is represented as a “$” sign. While writing JsonPath in RestAssured we do not need to add a dollar sign. The child node of a JSON object can be represented as a dot (.) or using [<childName>]. We can also use [] to represent a particular index element in a JSON array.
Let’s learn syntaxes with examples rather than theory.
[{ "id": 1, "first_name": "Lothaire", "last_name": "Benazet", "email": "lbenazet0@tinyurl.com", "gender": "Male" }, { "id": 2, "first_name": "Shellie", "last_name": "Cowser", "email": "scowser1@163.com", "gender": "Female" }, { "id": 3, "first_name": "Sharl", "last_name": "Hesbrook", "email": "shesbrook2@economist.com", "gender": "Female" }, { "id": 4, "first_name": "Merrili", "last_name": "Acom", "email": "macom3@goo.ne.jp", "gender": "Female" }, { "id": 5, "first_name": "Remus", "last_name": "Downgate", "email": "rdowngate4@shinystat.com", "gender": "Male" }, { "id": 6, "first_name": "Tatiana", "last_name": "Tribble", "email": "ttribble5@simplemachines.org", "gender": "Female" }, { "id": 7, "first_name": "Wood", "last_name": "Hebbes", "email": "whebbes6@psu.edu", "gender": "Male" }, { "id": 8, "first_name": "Kendall", "last_name": "Bony", "email": "kbony7@epa.gov", "gender": "Male" }, { "id": 9, "first_name": "Robinet", "last_name": "Gooday", "email": "rgooday8@boston.com", "gender": "Male" }, { "id": 10, "first_name": "Laural", "last_name": "Krzysztofiak", "email": "lkrzysztofiak9@sun.com", "gender": "Female" }]
Example Program
package JsonPathSyntax; import java.io.File; import io.restassured.path.json.JsonPath; public class SyntaxForJsonArray { public static void main(String[] args) { File jsonArrayFile = new File(System.getProperty("user.dir")+"\\src\\main\\resources\\jsonFiles\\jsonArraySyntaxDemo.json"); JsonPath jsonPath = JsonPath.from(jsonArrayFile); /* * To get a specific field value of an indexed element [0] will give you first * element in an array and using dot operator we are retrieving value of * firstName */ System.out.println("First name is first employee :"+ jsonPath.getString("[0].first_name")); // To get whole indexed element System.out.println("All details of first employee : " +jsonPath.getJsonObject("[0]")); /* * To get first names of all employees Since it is a JSON array and we are not * specifying index then it will pick firstName from each element and return as * a list. */ System.out.println("First name of all employees" + jsonPath.getList("first_name")); /* To get all first name of all females only * If we want to filter records based on conditions we can use find or findAll expression. * findAll will iterate through each element in array and match condition. "it" represent current element. * For each element it will check if gender is "female". If yes then take firstName of current element. * findAl returns a List. */ System.out.println("First name of all female employees : "+jsonPath.getList("findAll{it.gender == 'Female'}.first_name")); System.out.println("First name of all female employees : "+jsonPath.getList("findAll{it -> it.gender == 'Female'}.first_name")); /* To get first female name * If we want to get firstName of first female employee we can use find expression.*/ System.out.println("First name of first female employee : "+jsonPath.getString("find{it.gender == 'Female'}.first_name")); /* * We can also use relational operator like first name of all employees whose id is 5 or more */ System.out.println("First name of all employees whose id is 5 or more : " + jsonPath.getList("findAll{it.id >= 5}.first_name")); // we can use use and (&) operator - logical System.out.println("First name of all employees whose id is 5 or more but less than 8 : " + jsonPath.getList("findAll{it.id >= 5 & it.id < 8}.first_name")); // We can also use or (|) operator System.out.println("First name of all employees whose id is greater than 9 or gender is female : " + jsonPath.getList("findAll{it.id >= 9 | it.gender == 'Female'}.first_name")); // We can get size of array using size() or size System.out.println("Total number of employees : " + jsonPath.getString("size()")); } }
Output
First name is first employee :Lothaire All details of first employee : {id=1, first_name=Lothaire, last_name=Benazet, email=lbenazet0@tinyurl.com, gender=Male} First name of all employees[Lothaire, Shellie, Sharl, Merrili, Remus, Tatiana, Wood, Kendall, Robinet, Laural] First name of all female employees : [Shellie, Sharl, Merrili, Tatiana, Laural] First name of all female employees : [Shellie, Sharl, Merrili, Tatiana, Laural] First name of first female employee : Shellie First name of all employees whose id is 5 or more : [Remus, Tatiana, Wood, Kendall, Robinet, Laural] First name of all employees whose id is 5 or more but less than 8 : [Remus, Tatiana, Wood] First name of all employees whose id is greater than 9 or gender is female : [Shellie, Sharl, Merrili, Tatiana, Robinet, Laural] Total number of employees : 10
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.