Introduction
Do you know what is De-Serialization? It is the process of converting a JSON object to a POJO object. But do you know how to convert a JSON object to a Java object using Jackson API and Gson API in Rest-Assured? No worries, in this article we will learn about how to convert JSON Object into a Java object using Jackson API and Gson API.

Fine! So let us learn how to convert a JSON object to a Java object using Jackson API and Gson API.💫
De-Serialization – A JSON Object To A Java Object Using Jackson API
Let’s learn how to convert a JSON object to Java object using Jackson API.
About Jackson API
Jackson API is a fast JSON processor in Java. Jackson API allows us to do a lot of things, including serialization, deserialization, reading and writing JSON files.
We must include Jackson API in the Java project build path in order to use it. You can either download a jar file with transitive jars or add using Maven.
The most recent Jackson Databind dependency must first be added to your Maven project.
Class ObjectMapper
The Jackson API's most potent class is this one, and that is how long we will use it. A Java object can be converted to a JSON object and vice versa using the ObjectMapper class. This article will show you how to translate a Java object into a JSON object using the ObjectMapper class.
Target JSON string:-
{
"firstName": "Akshat",
"lastName": "Agarwal",
"subject": "Data structures and algorithms",
"school": "Coding Ninjas"
}
We must convert the preceding JSON string to a Java object. A class, such as one or more POJO classes, is required in order to create a Java object, depending on the requirement. Create a class with field names that are exactly (case-sensitively) the same as the node names in the preceding JSON string because by default, when parsing a JSON object into a Java object, the getter setter methods of the fields are what are used.
package SerializationDeserialization;
public class Student {
private String firstName;
private String lastName;
private String subject;
private String school;
// Getter and setter methods
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
}
For various purposes, the ObjectMapper class offers a variety of readValue() methods that are overloaded. Getting JSON from a file or as a string, which is what you'll typically see. This method will compare the fields in the passed POJO class with the fields in the JSON string and use setter methods to set values that are simple to retrieve using getter methods.
JSON String To POJO
For various purposes, the ObjectMapper class offers a variety of readValue() methods that are overloaded. Getting JSON from a file or as a string is what you'll typically see. This method will compare the fields in the passed POJO class to the fields in the JSON string and use setter methods to set values that are simple to retrieve using getter methods.
package SerializationDeserialization;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class DeserializeJsonToJava {
public static void main(String[] args) throws JsonMappingException, JsonProcessingException {
String JSONString = "{\r\n" +
" \"firstName\" : \"Akshat\",\r\n" +
" \"lastName\" : \"Agarwal\",\r\n" +
" \"subject\" : \"Data structures and algorithms\",\r\n" +
" \"school\" : \"Coding Ninjas\",\r\n" +
}";
ObjectMapper objectMapper = new ObjectMapper();
// Pass JSON string and the POJO class
Student studentObject = objectMapper.readValue(JSONString, Student.class);
// Now use getter method to retrieve values
String firstName = studentObject.getFirstName();
String lastName = studentObject.getLastName();
String subject = studentObject.getSubject();
String school = studentObject.getSchool();
System.out.println("Details of student is as below:-");
System.out.println("First Name : "+firsName);
System.out.println("Last Name : "+lastName);
System.out.println("Subject Name : "+subject);
System.out.println("School Name : "+school);
}
}
Output -
