Deserialization – How to convert JSON to Java Object using Jackson API

HOME

The previous tutorial explains the Serializarion process that means converting Java Objects to JSON Payload. This is done using Jackson API. This tutorial explains the Deserialization, means converting JSON Payload to Java Objects.

Deserialization ā€“ It is the reverse of serializing. In this process, we will read the Serialized byte stream from the file and convert it back into the Class instance representation. Here, we are converting a JSON Object to an Employee class object.

We are using Jackson API for Serialization and Deserialization. So, add the Jackson dependency to the project.

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.0</version>
</dependency>

Below is the sample code of the Employee table, which contains the data members needed for Employee JSON and their corresponding getter and setter methods.

public class Employee {
 
    // private variables or data members of POJO class
    private String firstName;
    private String lastName;
    private int age;
    private double salary;
    private String designation;
    private String contactNumber;
    private String emailId;
 
    // 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 int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
 
    public double getSalary() {
        return salary;
    }
 
    public void setSalary(double salary) {
        this.salary = salary;
    }
 
    public String getDesignation() {
        return designation;
    }
 
    public void setDesignation(String designation) {
        this.designation = designation;
    }
     
    public String getContactNumber() {
        return contactNumber;
    }
 
    public void setContactNumber(String contactNumber) {
        this.contactNumber = contactNumber;
    }
 
    public String getEmailId() {
        return emailId;
    }
 
    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }
 
}

Now, let us create a Test Class to show Deserialization.

    @Test
    public void deserializationTest() {
 
        Employee employee = new Employee();
        employee.setFirstName("Tim");
        employee.setLastName("Tran");
        employee.setAge(49);
        employee.setSalary(89000);
        employee.setDesignation("Manager");
        employee.setContactNumber("+3538944412341");
        employee.setEmailId("ttran@test.com");
 
        // Converting a Java class object to a JSON payload as string
        ObjectMapper mapper = new ObjectMapper();
        String employeeJson = null;
        try {
            employeeJson = mapper.writeValueAsString(employee);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
 
        // Converting Employee json string to Employee class object

        try {
            Employee employee2 = mapper.readValue(employeeJson, Employee.class);
            System.out.println("First Name of employee : " + employee2.getFirstName());
            System.out.println("Last Name of employee : " + employee2.getLastName());
            System.out.println("Age of employee : " + employee2.getAge());
            System.out.println("Salary of employee : " + employee2.getSalary());
            System.out.println("Designation of employee : " + employee2.getDesignation());
            System.out.println("Contact Number of employee : " + employee2.getContactNumber());
            System.out.println("EmailId of employee : " + employee2.getEmailId());
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

    }

We can read JSON String and convert it back to Java Object as shown below. We will use readValue() to deserialize JSON content from the given file into a given Java type.

This is my JSON saved in a file placed at Desktop.

Below is the Test

@Test
public void readJson() {
 
    ObjectMapper mapper = new ObjectMapper();
 
    // Converting Employee JSON string to Employee class object
    try {
          Employee employee2 = mapper.readValue(new File(
                "C:\\Users\\Vibha\\Desktop\\Employee.json"),
                Employee.class);
          System.out.println("First Name of employee : " + employee2.getFirstName());
          System.out.println("Last Name of employee : " + employee2.getLastName());
          System.out.println("Age of employee : " + employee2.getAge());
          System.out.println("Salary of employee : " + employee2.getSalary());
          System.out.println("Designation of employee : " + employee2.getDesignation());
          System.out.println("Contact Number of employee : " + employee2.getContactNumber());
          System.out.println("EmailId of employee : " + employee2.getEmailId());
    } catch (StreamReadException e) {
        e.printStackTrace();
    } catch (DatabindException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

I hope this has helped to clear your doubts regarding how to create Java Objects from JSON using Jackson API.

We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s