Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 61 – Deserialize Using JsonPath

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 deserialize using JsonPath.

Earlier we have seen De-Serialization – JSON Object To Java Object Using Jackson API. The same we can achieve using JsonPath as well.

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

We will take a very simple JSON Object and will create a POJO for it.

{
  "id": 1,
  "first_name": "Lothaire",
  "last_name": "Benazet",
  "email": "[email protected]",
  "gender": "Male"
}
package DeserializationUsingJsonPath;

public class Employee {

        private Integer id;
        private String first_name;
        private String last_name;
        private String email;
        private String gender;
        
        public Integer getId() {
                return id;
        }
        public void setId(Integer id) {
                this.id = id;
        }
        public String getFirst_name() {
                return first_name;
        }
        public void setFirst_name(String first_name) {
                this.first_name = first_name;
        }
        public String getLast_name() {
                return last_name;
        }
        public void setLast_name(String last_name) {
                this.last_name = last_name;
        }
        public String getEmail() {
                return email;
        }
        public void setEmail(String email) {
                this.email = email;
        }
        public String getGender() {
                return gender;
        }
        public void setGender(String gender) {
                this.gender = gender;
        }
}

Converting a Java object to a JSON object is called serialization and the reverse of it is called deserialization. JsonPath class provides a method named “T getObject(String path, Class objectType)” which helps to get the result of an Object path expression as a java Object.

package DeserializationUsingJsonPath;

import io.restassured.path.json.JsonPath;

public class DeserializationUsingJsonPath {
        
        public static void main(String[] args) {
                
                String jsonObject = "{\r\n" + 
                                "  \"id\": 1,\r\n" + 
                                "  \"first_name\": \"Lothaire\",\r\n" + 
                                "  \"last_name\": \"Benazet\",\r\n" + 
                                "  \"email\": \"[email protected]\",\r\n" + 
                                "  \"gender\": \"Male\"\r\n" + 
                                "}";
                
                JsonPath jsonPath = JsonPath.from(jsonObject);
                
                Employee employee = jsonPath.getObject("", Employee.class);
                System.out.println("Id is         : "+employee.getId());
                System.out.println("First name is : "+employee.getFirst_name());
                System.out.println("Last name is  : "+employee.getLast_name());
                System.out.println("Email id is   : "+employee.getEmail());
                System.out.println("Gender is     : "+employee.getGender());
                
                // Another way
                Employee employee1 = jsonPath.getObject("$", Employee.class);
                System.out.println("Id is         : "+employee1.getId());
                System.out.println("First name is : "+employee1.getFirst_name());
                System.out.println("Last name is  : "+employee1.getLast_name());
                System.out.println("Email id is   : "+employee1.getEmail());
                System.out.println("Gender is     : "+employee1.getGender());

        }

}
Id is         : 1
First name is : Lothaire
Last name is  : Benazet
Email id is   : [email protected]
Gender is     : Male
Id is         : 1
First name is : Lothaire
Last name is  : Benazet
Email id is   : [email protected]
Gender is     : Male

Above I used “” and “$” as JSON path string as we have a simple JSON Object. If we have a nested or JSON array then we can write a proper JSON path syntax and deserialize only the result of it.

Suppose we have a JSON array as below:-

[{
  "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"
}]

If we want to deserialize only the second JSON object from the above JSON array, we can do that as well.

package DeserializationUsingJsonPath;

import io.restassured.path.json.JsonPath;

public class DeserializationUsingJsonPath {
        
        public static void main(String[] args) {
                
                String jsonObject = "[{\r\n" + 
                                "  \"id\": 1,\r\n" + 
                                "  \"first_name\": \"Lothaire\",\r\n" + 
                                "  \"last_name\": \"Benazet\",\r\n" + 
                                "  \"email\": \"[email protected]\",\r\n" + 
                                "  \"gender\": \"Male\"\r\n" + 
                                "}, {\r\n" + 
                                "  \"id\": 2,\r\n" + 
                                "  \"first_name\": \"Shellie\",\r\n" + 
                                "  \"last_name\": \"Cowser\",\r\n" + 
                                "  \"email\": \"[email protected]\",\r\n" + 
                                "  \"gender\": \"Female\"\r\n" + 
                                "}]";
                
                JsonPath jsonPath = JsonPath.from(jsonObject);
                
                Employee employee = jsonPath.getObject("[1]", Employee.class);
                System.out.println("Id is         : "+employee.getId());
                System.out.println("First name is : "+employee.getFirst_name());
                System.out.println("Last name is  : "+employee.getLast_name());
                System.out.println("Email id is   : "+employee.getEmail());
                System.out.println("Gender is     : "+employee.getGender());
                
        }

}

Id is         : 2
First name is : Shellie
Last name is  : Cowser
Email id is   : [email protected]
Gender is     : Female

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: REST Assured Tutorial 69 – Response Logging In Rest Assured
Next Post: Log4j2 Tutorial 7 – Working Mechanism of Default Rollover Strategy

Related Posts

image – Make Selenium Easy Uncategorized
Part 1: Handling Drop-down Created Using SELECT Tag In Selenium Uncategorized
July 9, 2018 – Make Selenium Easy Uncategorized
List Of Frequently Asked Manual Testing Interview Questions( 200+)…. Uncategorized
Log4j2 Tutorial 6 – Introduction to RollingFileAppender and its Triggers and Strategies Uncategorized
REST Assured Tutorial 48 – How To Pass Headers In Rest Assured Requests 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