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

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.

Sample JSON Array Payload

[
  {
    "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.

Employee POJO

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("========================================================");
		}
	}
}

Output

[ {
  "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

Find all Selenium related post here, all API manual and automation related posts here and find frequently asked Java Programs here.

Many other topics you can navigate through menu.

1 thought on “REST Assured Tutorial 30 – How To Create POJO Classes Of A JSON Array Payload

Leave a Reply

Your email address will not be published. Required fields are marked *