Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 60 – Learn to write JsonPath expressions or JsonPath syntax

Posted on 02/19/2025 By admin

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?

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

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 []. 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": "[email protected]",
  "gender": "Male"
}, {
  "id": 2,
  "first_name": "Shellie",
  "last_name": "Cowser",
  "email": "[email protected]",
  "gender": "Female"
}, {
  "id": 3,
  "first_name": "Sharl",
  "last_name": "Hesbrook",
  "email": "[email protected]",
  "gender": "Female"
}, {
  "id": 4,
  "first_name": "Merrili",
  "last_name": "Acom",
  "email": "[email protected]",
  "gender": "Female"
}, {
  "id": 5,
  "first_name": "Remus",
  "last_name": "Downgate",
  "email": "[email protected]",
  "gender": "Male"
}, {
  "id": 6,
  "first_name": "Tatiana",
  "last_name": "Tribble",
  "email": "[email protected]",
  "gender": "Female"
}, {
  "id": 7,
  "first_name": "Wood",
  "last_name": "Hebbes",
  "email": "[email protected]",
  "gender": "Male"
}, {
  "id": 8,
  "first_name": "Kendall",
  "last_name": "Bony",
  "email": "[email protected]",
  "gender": "Male"
}, {
  "id": 9,
  "first_name": "Robinet",
  "last_name": "Gooday",
  "email": "[email protected]",
  "gender": "Male"
}, {
  "id": 10,
  "first_name": "Laural",
  "last_name": "Krzysztofiak",
  "email": "[email protected]",
  "gender": "Female"
}]
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()"));
                
                
        }
        
        

}
First name is first employee :Lothaire
All details of first employee : {id=1, first_name=Lothaire, last_name=Benazet, [email protected], 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

Uncategorized

Post navigation

Previous Post: Linux Basic Commands – date
Next Post: Interview Experience at Harman Connected Services Bangalore for Manual & Automation Testing Profile – Aug-2019

Related Posts

February 8, 2018 – Make Selenium Easy Uncategorized
March 2018 – Make Selenium Easy Uncategorized
RegisterPostman – Make Selenium Easy Uncategorized
How To Push Code From Eclipse To GitHub Repo Using Access Token? Uncategorized
TestNG Tutorials 52: DataProvider in TestNG – Accessing DataProvider Methods From Another Class Test Class | Make Selenium Easy Uncategorized
Part 5: Usages Of Javascripts In Selenium: Understanding Method executeAsyncScript Of JavascriptExecutor 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