Make Selenium Easy

REST Assured Tutorial 29 – How to create POJO classes of a JSON Object 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 Object payload.

POJO classes are extensively used for creating JSON and XML payloads for API. Although there are many online platform to generate POJO and Java libraries to generate POJO classes automatically but still knowing to create POJO helps. Even you can use plain POJO concepts or POJO with builder pattern for data storage as well which we have seen in last post where I have stored employee data.

Although there is no restrictions on the way of creating POJO but creating with some guidelines help which we will see in this post.

Very simple JSON example

Below is a JSON with some nodes which is actually a 1:1 mapping i.e. each key has single value and type of values are mixed.

Let’s start creating POJO step by step.

Identify fields or variables for POJO class

Observe in example JSON, we have fields firstName, lastName , gender, age , salary and married. Each field has a corresponding values. Now focus on data types of values. firstName and lastName has String values while age has integer value, salary has double value and married has boolean. Let’s map data type with field name as below :-

Let’s create variables in POJO class now. Since it is storing a employee detail, name POJO class as Employee and make all variables (at least for this example and it is beginning ) as private so that no one can manipulate it directly.

Add getter and setter methods for private variables

Since we have created all variables as private then there should be a way to manipulate or retrieve these data. Let’s add getter and setter methods for each variables. It is not mandatory to have getter and setter for all and depends upon need. But as of now let’s add getter and setter for all in same class. A getter method is to get value of a variable and a setter method is to set value of a variable from outside.

If you are worrying that it is painful to create getter and setter methods manually for a large number of variables then do not worry. Every IDE gives you a shortcut to generate getter and setter methods. Just google it out.

Let’s do not add any constructors in class so that it will have a default constructors.

Complete POJO class

We are done now. We have created a POJO class of employee JSON. Now you must be thinking how this POJO class will help now?

Using above POJO class you can create any number of custom Employee objects and each object can be converted in to a JSON Object and Each JSON object can be parsed in to Employee POJO. If you have an API which requires dynamic payload of Employee then you can easily create as many as required employee payloads with different data in stead of creating hard coded JSON objects. In simple words POJO gives you flexibility of creating and manipulating data in simple ways.

We will create a JSON object form POJO and vice versa now which is generally called as serialization and deserialization using Jackson APIs. Do not worry about the terms serialization and deserialization as of now as I have not covered it yet. For time being, you just know :-

serialization – Convert Employee class object to JSON representation or Object

deserialization – reverse of serializing . Convert a JSON Object to Employee class object


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.

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.

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

9 thoughts on “REST Assured Tutorial 29 – How to create POJO classes of a JSON Object Payload

  1. Hi Amod,
    Can you please explain the steps for XML also. In json I learned how to set and get dynamic variable by reading your blogs. So I need to know how we will perform for XML file and how we will get the dynamic field or variables . What are the steps ?
    How we can proceed?

  2. Can we not pass a specific object from a pojo at runtime? example if i am three objects in pojo while initializing but during runtime i have a condition and i want to pass only 2 object from the pojo which has 3 objects. Can this be achieved?

  3. very nicely explained..I am new to this concept. Do u mind sharing the maven dependencies u hv used here ?

  4. Good one thanks for explaining this concept of seralization and deserialisation for json.

    and yes would like to know as Rahul Krishna has asked too, why we named it as POJO, its a class concept i.e. encapsulation and why we have named object with specific names like Amod

    why not generic names like employee because we might have 100 employees, so in that case will we create 100 objects with 100 unique names ?

  5. Amod ,if we have a objectmapper.createObjectNode() method which create a objectNode and we pass the same object to objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(), and in pojo we are skipping this step of creating objectNode ,rather directly passing our POJO to the objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(), then whats the difference or advantage of using POJO vs creating a objectNode and then passing it .In either way we are getting the JSON String .

  6. Hi Amod,

    Very elaborated and free flow explanation above. I would recommend you to provide library/dependency details in form of download link/maven/gradle. Like, here you have mentioned about usage of Jackson APIs. Even though if you have already mentioned corresponding link in some other post, but still it will help readers to have it handy. It is just my personal opinion.


Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.