Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 58 – What is JsonPath and how to create it for simple and nested JSON Object?

Posted on 01/13/2025 By admin

As a part of the End to End REST Assured Tutorial, in this post, we will learn about JsonPath.

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. What is JsonPath?
  2. How to write JsonPath for a simple JSON object?
  3. How to write JsonPath for a nested JSON object?

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

JSON stands for “JavaScript Object Notation” which is a lightweight, language-independent, and self-describing format in-text for data storing and interchange. JSON is easy to read, write and create for humans which makes it famous over XML. To know more about JSON refer to my previous post – Introduction To JSON

A JSON consists of JSON Nodes or JSON elements. To traverse to a JSON element there will be a path. For example- Consider the below postal address:-

#404 , No More Apartment,
Gumnam Gali,
Bengaluru,
Karnataka, India

Actually, the above address shows a path to reach there. You need to go to India -> Karnataka -> Bengaluru -> Gumnma Gali -> No More apartment -> #404.

Similarly, a JsonPath represents a path to reach a JSON element and extract its value.

There are different syntaxes to write JsonPath but let’s start with a very simple JSON Object.

{
  "firstName": "Amod",
  "lastName": "Mahajan"
}

In the above JSON Object, we have two JSON elements or nodes “firstName” and “lastName” enclosed in two curly braces. To reach node “firstName” we need to start from the top curly brace. In fact, in any JSON object, there will be a root node which is represented as “$”(dollar). From this root node, we need to start to traverse through a JSON element.

Since there is a root node and “firstName” and “lastName” are children (direct) or child nodes of the root node. To reference a child node of a parent node we need to use the dot (.) symbol.

JsonPath for node “firstName” – $.firstName

JsonPath for node “lastName” – $.lastName

Whenever we write above JsonPath in RestAssured scripts, we don’t include dollar($) sign as it expects JsonPath has a root node.

To use JsonPath with RestAssured example, we need to get JsonPath instance of JSON Object, and then we can use respective methods to get the value of JSON element.

package JsonPathForJsonObject;

import io.restassured.path.json.JsonPath;

public class SimpleJsonObject {
        
        public static void main(String[] args) {
                
                String jsonString = "{\r\n" + 
                                "  \"firstName\": \"Amod\",\r\n" + 
                                "  \"lastName\": \"Mahajan\"\r\n" + 
                                "}";
                
                //Get JsonPath instance of above JSON string
                JsonPath jsonPath = JsonPath.from(jsonString);
                
                // Since firstName holds a string value use getString() method and provide json path of firstName
                String firstName = jsonPath.getString("firstName");
                String lastName = jsonPath.getString("lastName");
                
                System.out.println("First name is : "+firstName);
                System.out.println("Last name is : "+lastName);
                
                // Since $ is root node of a JSON, we can get whole JSON string using $ as JSON path
                System.out.println(jsonPath.getString("$"));
                // There are two other ways to do the same thing as above
                System.out.println(jsonPath.getString(""));
                System.out.println(jsonPath.get());
                
        }

}
First name is : Amod
Last name is : Mahajan
[firstName:Amod, lastName:Mahajan]
[firstName:Amod, lastName:Mahajan]
{firstName=Amod, lastName=Mahajan}

Let’s take the below example where I have nested JSON elements. JSON element “houseNo” is child node of “address”. JSON element “name” is a child node of “language” which is a child node of “skills”. This type of JSON object is called nested JSON object.

{
  "firstName": "Amod",
  "lastName": "Mahajan",
  "address": {
    "houseNo": 404,
    "buildingName": "Not Found",
    "streetName": "Gumnam gali",
    "city": "Bengaluru",
    "state": "Karnataka",
    "country": "India"
  },
  "skills": {
    "language": {
      "name": "Java",
      "proficiency": "Medium"
    }
  }
}

If we want to write JSON path for node “houseNo” then we need to traverse as below:-

root node ($) -> address -> houseNo

Since we represent a child of a parent using dot (.) the final JSONPath will be:-

$.address.houseNo

Similarly, for node “name”, JSON Path will be:-

$.skills.language.name

package JsonPathForJsonObject;

import io.restassured.path.json.JsonPath;

public class NestedJsonObject {
        
        public static void main(String[] args) {
                
                String jsonString = "{\r\n" + 
                                "  \"firstName\": \"Amod\",\r\n" + 
                                "  \"lastName\": \"Mahajan\",\r\n" + 
                                "  \"address\": {\r\n" + 
                                "    \"houseNo\": 404,\r\n" + 
                                "    \"buildingName\": \"Not Found\",\r\n" + 
                                "    \"streetName\": \"Gumnam gali\",\r\n" + 
                                "    \"city\": \"Bengaluru\",\r\n" + 
                                "    \"state\": \"Karnataka\",\r\n" + 
                                "    \"country\": \"India\"\r\n" + 
                                "  },\r\n" + 
                                "  \"skills\": {\r\n" + 
                                "    \"language\": {\r\n" + 
                                "      \"name\": \"Java\",\r\n" + 
                                "      \"proficiency\": \"Medium\"\r\n" + 
                                "    }\r\n" + 
                                "  }\r\n" + 
                                "}";
                
                //Get JsonPath instance of above JSON string
                JsonPath jsonPath = JsonPath.from(jsonString);
                
                // Since houseNo holds an int value use getInt() method and provide json path of houseNo
                int houseNo = jsonPath.getInt("address.houseNo");
                System.out.println("House no is : "+houseNo);
                
                String name = jsonPath.getString("skills.language.name");
                System.out.println("Name is : "+name);
                
        }

}
House no is : 404
Name is : Java

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: TestNG Tutorials 68 : Sharing Data Between Tests in a Suite Using ISuite & ITestContext
Next Post: TestNG Tutorials 54: DataProvider in TestNG – Implementing Object Oriented Concept “Encapsulation” with DataProvider Method

Related Posts

Untitled Diagram.jpg – Make Selenium Easy Uncategorized
Replace Value in JSON Using JsonPath in Java | Jayway JsonPath | Rest Assured | Uncategorized
Postman Tutorial Part 51 -Printing Request Body & Response Body in Postman Console Uncategorized
Handling Browser Window In Selenium Webdriver Uncategorized
January 2018 – Make Selenium Easy Uncategorized
July 17, 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