REST Assured Tutorial 61 – Deserialize Using JsonPath

Introduction

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.

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

Deserialization using JsonPath

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

JSON Object Example

{
  "id": 1,
  "first_name": "Lothaire",
  "last_name": "Benazet",
  "email": "lbenazet0@tinyurl.com",
  "gender": "Male"
}

Employee POJO

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.

Deserialization example

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());

	}

}
Output
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

JSON Array example

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.

Deserialization example for JSON array

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());
		
	}

}

Output
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

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.

1 thought on “REST Assured Tutorial 61 – Deserialize Using JsonPath

  1. Hi,
    Please could you help me? I don’t know how to deserialize this

    {
    “status”: {
    “timestamp”: “2022-01-18T15:27:20.907Z”,
    “error_code”: 0,
    “error_message”: null,
    “elapsed”: 16,
    “credit_count”: 1,
    “notice”: null
    },
    “data”: [
    {
    “id”: 1,
    “name”: “Bitcoin”,
    “symbol”: “BTC”,
    “slug”: “bitcoin”,
    “rank”: 1,
    “is_active”: 1,
    “first_historical_data”: “2013-04-28T18:47:21.000Z”,
    “last_historical_data”: “2022-01-18T15:19:00.000Z”,
    “platform”: null
    },
    {
    “id”: 1027,
    “name”: “Ethereum”,
    “symbol”: “ETH”,
    “slug”: “ethereum”,
    “rank”: 2,
    “is_active”: 1,
    “first_historical_data”: “2015-08-07T14:49:30.000Z”,
    “last_historical_data”: “2022-01-18T15:19:00.000Z”,
    “platform”: null
    }
    ]
    }

    I created POJO class Coin and now have to to populate it. Your example didn’t help me. And I don’t know how to bypass “status”

Leave a Reply

Your email address will not be published. Required fields are marked *