HomeJavaEasy way to convert JSON to Java POJO

Easy way to convert JSON to Java POJO

#Introduction

In this journal, we will be having a look at how we can easily convert JSON to Java POJO. All the experienced Java developers must be knowing what a POJO is, but for beginners, POJO is a “Plain Old Java Object”. In other words we can say that POJO is a plain Java class with members.

#Conversion

We will be using a fake RESTful service to understand this conversion process. This fake RESTful service is providing the data in the JSON format. We will try to use this data and convert it in Java POJO class.

Here is the link provided for the fake RESTful service: https://jsonplaceholder.typicode.com/.

Lets open this URL in the browser and check out the end points. Scroll down on the JSON Placeholder page and you will find the resources.

jsonplaceholder endpoints
jsonplaceholder endpoints

These are all the available end points. You can use them in your application. In this example we are going to use /Posts end point. To check the JSON data first, click on the /posts link and you will see json result. Please go ahead and copy the URL, we will be requiring this end point.

After hitting the /Post end point you can see the result shown below this is in json and we going to convert this in Java POJO class.

jsonplaceholder-posts-data
jsonplaceholder-posts-data

We are going to use this endpoint and convert the JSON data into Java class.

The easiest way to achieve this target is done via “json2java”. Go ahead and open this link and paste the copied endpoint in the box provided. You will see the magic happen and this tool automatically generates Java POJO class from the JSON data.

jsonplaceholder-java-pojo
jsonplaceholder-java-pojo
You can also provide json manually to convert it into Java class. Or you can paste the URL which contains json. Then click on convert to generate Java class.
RELATED ARTICLES

Most Popular