Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 33 – De-Serialization – JSON Object To Java Object Using Jackson API

Posted on 03/16/2025 By admin

As a part of End to End REST Assured Tutorial, in this post, We will learn the “De-Serialization” concept where we will create Java Object from JSON Objects.

Previously we have learned Serialization – Java Object To JSON Object Using Jackson API.

We have learned about POJOs in the previous posts. If you have not refereed those posts then I will advise you to go through about POJO concepts here:-

What Is Plain Old Java Object (POJO)?

How To Create POJO Classes Of A JSON Object Payload?

How To Create POJO Classes Of A JSON Array Payload?

How To Create POJO Classes Of A Nested JSON Payload?

When we request an API it may return a response body which is mostly JSON or XML. We can use Jsonpath or Xmlpath to traverse through nodes to fetch specific values but it is not advisable if the response is bigger or heavily nested. We can parse that JSON or XML response into POJO classes. After parsing into POJO classes, we can easily get values from response easily. This is called De-serialization. For this, we can use any JSON parser APIs. In this post, we will use Jackosn APIs which is more famous. I will also cover using the GSON library later.

Jackson API is a high-performance JSON processor for Java. We can perform serialization, deserialization, reading a JSON file, writing a JSON file, and a lot more things using Jackson API.

To use Jackson API, we need to add it to the Java project build path. You can add using Maven or download a jar file with transitive jars.

Let’s first add the latest Jackson Databind dependency in your maven project. I have used below dependency for this post which is the latest version:-

  com.fasterxml.jackson.core jackson-databind 2.11.0


Note:- When we add jackosn-databind dependency then it will automatically download transitive dependencies of the same version i.e. jackson-annotations and jackson-core as well.

If you download and add jackson-databind jar to build path, do not forget to download the other two transitive dependencies as well.

This is the most powerful class provided by Jackson API and maximum time we will use this class. ObjectMapper class is useful to create JSON object, JSON Array, converting a Java object to a JSON object and vice versa. In this post, we will use the ObjectMapper class to convert a Java Object to a JSON object.

{
  "firstName" : "Amod",
  "lastName" : "Mahajan",
  "gender" : "M",
  "age" : 29,
  "salary" : 10987.77,
  "married" : false
}

We need to parse above JSON string to Java Object. To create a Java object we need a class i.e. one or multiple POJO classes based on the requirement. Let’s create a class with field name exactly (case sensitive) the same as node names in above JSON string because with default setting while parsing JSON object o Java object, it will look on getter setter methods of field names. In fact, access specifiers do not matter here. We will cover some interesting facts on it later.

package SerializationDeserialization;

public class Employee {

        private String firstName;
        private String lastName;
        private String gender;

        private int age;
        private double salary;
        private boolean married;

        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 getGender() {
                return gender;
        }

        public void setGender(String gender) {
                this.gender = gender;
        }

        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 boolean getMarried() {
                return married;
        }

        public void setMarried(boolean married) {
                this.married = married;
        }

}

Yup! ObjectMapper class provides various overloaded readValue() methods for different purposes. Reading JSON from a file or as a string which you will see mostly. This method will match fields of JSON string to passed POJO class fields and set values using setter methods which we can easily 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\" : \"Amod\",\r\n" + 
                                "  \"lastName\" : \"Mahajan\",\r\n" + 
                                "  \"gender\" : \"M\",\r\n" + 
                                "  \"age\" : 29,\r\n" + 
                                "  \"salary\" : 10987.77,\r\n" + 
                                "  \"married\" : false\r\n" + 
                                "}";
                
                ObjectMapper objectMapper = new ObjectMapper();
                // Pass JSON string and the POJO class 
                Employee employeeObject = objectMapper.readValue(jsonString, Employee.class);
                // Now use getter method to retrieve values
                String firsName = employeeObject.getFirstName();
                String lastName = employeeObject.getLastName();
                String gender = employeeObject.getGender();
                int age = employeeObject.getAge();
                double salary = employeeObject.getSalary();
                boolean married = employeeObject.getMarried();
                
                System.out.println("Details of Employee is as below:-");
                System.out.println("First Name : "+firsName);
                System.out.println("Last Name : "+lastName); 
                System.out.println("Gender : "+gender);
                System.out.println("Age : "+age);
                System.out.println("Salary : "+salary);
                System.out.println("Married : "+married);
        }
}

You can download/clone the above sample project from here.

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: Why syntax is driver.manage().window().maximize()?
Next Post: testng

Related Posts

Postman Tutorial Part 41 – Dynamic Variables in Postman – Make Selenium Easy Uncategorized
findElement Uncategorized
Protractor Tutorial 1 – What is AngularJS and Angular Applications? Uncategorized
singleHtml – Make Selenium Easy Uncategorized
get Uncategorized
Behavior Driven Development (BDD) – Understand in Easy Words Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark