Hire QA – Specialized in QA Recruitment, Technical Interviews and Testing Solutions

How To Create POJO Classes Of A JSON Array Payload

Category: Rest Assured

Simple Json Array Object:

[
{
“firstName”: “Phani”,
“lastName”: “Nagula”,
“gender”: “Male”,
“age”: 45,
“salary”: 50000,
“married”: true
}
]
Create POJO Class:

package RestAssuredPojo;
 
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;
	} 
}
package RestAssuredPojo;
 
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 phani = new Employee();
		amod.setFirstName("Phani");
		amod.setLastName("Nagula");
		amod.setAge(45);
		amod.setGender("Male");
		amod.setSalary(50000);
		amod.setMarried(true);
 

// Creating a List of Employees
		List allEmployees = new ArrayList();
		allEmployees.add(phani );

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

Leave a Reply

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