How to write POJO classes for deserialization of a JSON response and extract the information


How to write POJO classes for  deserialization of a JSON response and extract the information

Hi, all welcome to another of my lockdown blog series posts.  There are multiple ways in which we can retrieve data from a JSON response that we get when working with RestAssured and one such way is to write POJO classes in Java to serialize and deserialize and call elements as objects. In this blog post, we will look at how we can take an API response and de-serialize the body to a POJO. For our example, we will use the API endpoint [1]

Let's say we have a JSON response as below in this response we have root-level elements ex: weather and child elements ex: description and our objective is to wite POJO classes to deserialize these.

{
   "coord":{
      "lon":-0.13,
      "lat":51.51
   },
   "weather":[
      {
         "id":300,
         "main":"Drizzle",
         "description":"light intensity drizzle",
         "icon":"09d"
      }
   ],
   "base":"stations",
   "main":{
      "temp":280.32,
      "pressure":1012,
      "humidity":81,
      "temp_min":279.15,
      "temp_max":281.15
   },
   "visibility":10000,
   "wind":{
      "speed":4.1,
      "deg":80
   },
   "clouds":{
      "all":90
   },
   "dt":1485789600,
   "sys":{
      "type":1,
      "id":5091,
      "message":0.0103,
      "country":"GB",
      "sunrise":1485762037,
      "sunset":1485794875
   },
   "id":2643743,
   "name":"London",
   "cod":200

When writing POJO classes we need to write classes for the root level and for each of the child elements. As shown below for all elements within our parent class we have declared variables and then we have declared getters and setter methods. Where we have child elements, those variables are declared as an object type rather than a primitive type.

package weather;
import java.util.List;

public class Serialization {

private Coord coord;
private List<Weather> weather;
private String base;
private MainClass main;
private String visibility;
private Wind wind;
private Clouds clouds;
private String dt;
private Sys sys;
private String id;
private String name;
private String cod;

public Coord getCoord() {
return coord;
}

public void setCoord(Coord coord) {
this.coord = coord;
}

public List<Weather> getWeather() {
return weather;
}

public void setWeather(List<Weather> weather) {
this.weather = weather;
}
}

The below code is how we can write a POJO class for the child element Weather.


package weather;

public class Weather {

private int id;
private String description;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

}

Once our classes are written the next step for deserialization is to extract the data as shown below within our test class. Using the Rest Assured syntax we write .get().as() and pass the name of the class which has the required getter methods to help with the deserialization. To test if we can extract the values I have written sysouts that will call the required getter methods and print the required data.

@Test
public void testWeatherResponse() {

RestAssured.baseURI ="https://samples.openweathermap.org/data/2.5/";

Serialization serialization= given().
queryParam("q", "London,UK").
queryParam("appid", "2b1fd2d7f77ccf1b7de9b441571b39b8").expect()
.defaultParser(Parser.JSON).
when()
.get("/weather/").as(Serialization.class);


System.out.println(">>>>>City Name>>>>>>"+serialization.getName());
System.out.println(">>>>>Coordination Latitude>>>>>>"+serialization.getCoord().getLat());
System.out.println(">>>>>Coordination Long>>>>>>"+serialization.getCoord().getLon());
System.out.println(">>>>>Base>>>>>>"+serialization.getBase());
System.out.println(">>>>>Temp>>>>>>"+serialization.getMain().getTemp());
System.out.println(">>>>>Visibility>>>>>>"+serialization.getVisibility());
System.out.println(">>>>>Weather>>>>>>"+serialization.getWeather().get(0).getDescription());
System.out.println(">>>>>Weather>>>>>>"+serialization.getWeather().get(0).getMain());
}


When we run the test class we should see outputs as below once the data are deserialized with the POJO classes.

>>>>>City Name>>>>>>London
>>>>>Coordination Latitude>>>>>>51.51
>>>>>Coordination Long>>>>>>-0.13
>>>>>Base>>>>>>stations
>>>>>Temp>>>>>>280.32
>>>>>Visibility>>>>>>10000
>>>>>Weather>>>>>>light intensity drizzle
>>>>>Weather>>>>>>Drizzle

Note: For ease of readability I have only added a sub section of the full set of POJO classes written for the above JSON response.

[1] https://samples.openweathermap.org/data/2.5/






Comments