Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 30 – How To Create POJO Classes Of A JSON Array Payload

Posted on 04/23/2025 By admin

As a part of End to End REST Assured Tutorial , in this post We will learn to create a simple POJO class for a simple JSON Array payload. In previous post we have learnt about How to create POJO classes of a JSON Object Payload.

If you are not aware of JSON Array then please refer Introduction to JSON here.

We will take similar JSON object payload as previous post but this time it will be multiple JSON Objects in a JSON Array.

[
  {
    "firstName": "Amod",
    "lastName": "Mahajan",
    "gender": "Male",
    "age": 28,
    "salary": 10000.56,
    "married": false
  },
  {
    "firstName": "Animesh",
    "lastName": "Prashant",
    "gender": "Male",
    "age": 30,
    "salary": 20000.56,
    "married": true
  },
  {
    "firstName": "Kitty",
    "lastName": "Gupta",
    "gender": "Female",
    "age": 26,
    "salary": 30000.56,
    "married": false
  }
]

Above JSON Array is a collection of JSON Objects or a List of Employees. We have created a Employee POJO class in previous post and we can reuse the same. That is how reusability is one of the major advantage of POJO classes.

package RestfulBookerPojo;

public class Employee {

        // private variables or data members of pojo class
        private String firstName;
        private String lastName;
        private String gender;
        private int age;
        private double salary;
        private boolean married;
        
        // 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 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;
        } 
}

We do not need any other classes. We just need to create a List of Employees . Below code snippets shows how can we convert a Java Object in to a JSON Array and vice versa.

package RestfulBookerPojo; import java.util.ArrayList;
import java.util.List; import org.testng.annotations.Test; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; public class ListOfEmployeesSerializationDeserialization { public String allEmployeeJson; @Test public void createListOfEmployeesJSONArrayFromEmployeePOJOClass() throws JsonProcessingException { // Create first employee Employee amod = new Employee(); amod.setFirstName("Amod"); amod.setLastName("Mahajan"); amod.setAge(29); amod.setGender("Male"); amod.setSalary(10000.56); amod.setMarried(false); // Create second employee Employee animesh = new Employee(); animesh.setFirstName("Animesh"); animesh.setLastName("Prashant"); animesh.setAge(30); animesh.setGender("Male"); animesh.setSalary(20000.56); animesh.setMarried(true); // Create third employee Employee kitty = new Employee(); kitty.setFirstName("Kitty"); kitty.setLastName("Gupta"); kitty.setAge(27); kitty.setGender("Female"); kitty.setSalary(30000.56); kitty.setMarried(false); // Creating a List of Employees List allEMployees = new ArrayList(); allEMployees.add(amod); allEMployees.add(animesh); allEMployees.add(kitty); // Converting a Java class object to a JSON Array payload as string ObjectMapper objectMapper = new ObjectMapper(); allEmployeeJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(allEMployees); System.out.println(allEmployeeJson); } @Test public void getPojoFromEmployeeObject() throws JsonProcessingException { // Converting EMployee json Array string to Employee class object ObjectMapper objectMapper = new ObjectMapper(); List allEmploeesDetails = objectMapper.readValue(allEmployeeJson, new TypeReference>() { }); for (Employee emp : allEmploeesDetails) { System.out.println("========================================================"); System.out.println("First Name of employee : " + emp.getFirstName()); System.out.println("Last Name of employee : " + emp.getLastName()); System.out.println("Age of employee : " + emp.getAge()); System.out.println("Gender of employee : " + emp.getGender()); System.out.println("Salary of employee : " + emp.getSalary()); System.out.println("Marital status of employee : " + emp.getMarried()); System.out.println("========================================================"); } }
} 
[ {
  "firstName" : "Amod",
  "lastName" : "Mahajan",
  "gender" : "Male",
  "age" : 29,
  "salary" : 10000.56,
  "married" : false
}, {
  "firstName" : "Animesh",
  "lastName" : "Prashant",
  "gender" : "Male",
  "age" : 30,
  "salary" : 20000.56,
  "married" : true
}, {
  "firstName" : "Kitty",
  "lastName" : "Gupta",
  "gender" : "Female",
  "age" : 27,
  "salary" : 30000.56,
  "married" : false
} ]
========================================================
First Name of employee : Amod
Last Name of employee : Mahajan
Age of employee : 29
Gender of employee : Male
Salary of employee : 10000.56
Marital status of employee : false
========================================================
========================================================
First Name of employee : Animesh
Last Name of employee : Prashant
Age of employee : 30
Gender of employee : Male
Salary of employee : 20000.56
Marital status of employee : true
========================================================
========================================================
First Name of employee : Kitty
Last Name of employee : Gupta
Age of employee : 27
Gender of employee : Female
Salary of employee : 30000.56
Marital status of employee : false
========================================================

You can download/clone 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: REST Assured Tutorial 39 – @JsonIgnore Annotation of Jackson API – Exclude Field of Pojo From Serialization and Deserialization
Next Post: Learn Selenium With Quiz – Basic Level 2

Related Posts

image – Make Selenium Easy Uncategorized
Selenium Interview Question 3 – Difference Between get() and navigate() Methods of Selenium WebDriver Uncategorized
Selenium Topics – Page 37 Uncategorized
Amod Mahajan – Page 2 – Make Selenium Easy Uncategorized
Interview Experience At Morgan Stanley Mumbai For SDET – Sep – 2020 Uncategorized
Postman Tutorial Part 39 – Header Presets in Postman 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