REST Assured Tutorial 61 – Deserialize Using JsonPath

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": "lbenazet0@tinyurl.com",
  "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\": \"lbenazet0@tinyurl.com\",\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   : lbenazet0@tinyurl.com
Gender is     : Male
Id is         : 1
First name is : Lothaire
Last name is  : Benazet
Email id is   : lbenazet0@tinyurl.com
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": "lbenazet0@tinyurl.com",
  "gender": "Male"
}, {
  "id": 2,
  "first_name": "Shellie",
  "last_name": "Cowser",
  "email": "scowser1@163.com",
  "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\": \"lbenazet0@tinyurl.com\",\r\n" + 
                                "  \"gender\": \"Male\"\r\n" + 
                                "}, {\r\n" + 
                                "  \"id\": 2,\r\n" + 
                                "  \"first_name\": \"Shellie\",\r\n" + 
                                "  \"last_name\": \"Cowser\",\r\n" + 
                                "  \"email\": \"scowser1@163.com\",\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   : scowser1@163.com
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