Javatpoint Logo
Javatpoint Logo

Convert Java object to JSON

JSON stands for JavaScript Object Notation that is a very small weighted format to store and transport data. The JSON format is very easy to understand because it has a self-describing nature. JSON is mainly used to transmit data between a server and a web application.

The details of an employee written in the JSON format looks like:

Java object is an instance of a class or a self-contained component having properties and methods for making a certain type of data useful. Java object has a state, behavior and identity.

Java object of an employee is written in the following way:

Many times we need to convert Java objects into JSON so that we can easily understand them. To convert a Java object into a JSON object, we have the following two methods or way:

  1. Using GSON library
  2. Using Jackson API
Convert Java object to JSON

Let's understand both of them one by one.

Using GSON library

The GSON library plays an important role in converting a Java object into JSON. GSON library not only converts Java object into JSON but also uses it to convert the JSON string into its equivalent Java object.

These are the following steps to convert a Java object into JSON:

  1. Create a Maven project.
  2. Add GSON dependency in xml file.
  3. Create Plain Old Java Object to convert into JSON.
  4. Create a Java class to convert the Java object to JSON.
Convert Java object to JSON

Step 1: Create a Maven project

In the first step, we need to create a maven project using eclipse IDE. Converting Java objects into JSON is an advanced Java concept, so we recommend you to use an IDE like eclipse to easily understand the programs.

Convert Java object to JSON

Step 2: Add GSON dependency in pom.xml

In the next step, we need to add the following GSON dependency in the pom.xml file to use the GSON library.

After adding the dependency, the pom.xml file look like the following:

Convert Java object to JSON

Step 3: Create POJO to convert into JSON

In the next step, we need to create a POJO, which we want to convert into a JSON object. In our example, we create a class MobilePhone with getter and setters for brand, name, ram, and rom.

MobilePhone.java

Step 4: Create a Java class to convert the Java object into JSON

We need to create a new Java class that uses GSON to convert the MobilePhone object to JSON. In this class, we will create the getMobile() method. Inside the method, we set the values to the MobilePhone class's properties and return the MobilePhone object.

In the main method, we will create an object of the MobilePhone class and store the geMobile() method's returned value. After that, we will use the Gson().toJson() method to convert that object into a JSON object.

ConvertJSON.java

When we run the ConvertJSON.java file, we will see the JSON object of the MobilePhone class object.

Convert Java object to JSON

Using Jackson library

Jackson library is one of the simplest Java-based libraries. The Jackson library is used to serialize the Java object to JSON and vice versa. The ObjectMapper class of the Jackson API provides methods to convert the Java object to JSON format or object. The ObjectMapper class writeValueAsString() method takes the JSON object as a parameter and returns its respective JSON string.

These are the following steps to convert the Java object into a JSON object using Jackson API:

  1. Create a Maven project
  2. Add Jackson dependency to the pom.xml file.
  3. Create a POJO object.
  4. Create a new class to convert Java object to JSON object.
Convert Java object to JSON

Step 1: Create a Maven project

In the first step, we need to create a maven project using eclipse IDE. Just like GSON, Jackson API is also supported by the Eclipse.

Convert Java object to JSON

Step 2: Add Jackson dependency in pom.xml

In the next step, we need to add the following Jackson dependency in our pom.xml file to use the Jackson library.


Convert Java object to JSON

Step 3: Create POJO to convert into JSON

In the next step, we need to create a POJO, which we want to convert into a JSON object. In this example, we create a class Product having getter and setters for the properties like id, name, and price.

Product.java

Step 4: Create a Java class to convert the Java object into JSON

Just like our previous example of GSON, we will also create a new Java class ObjectToJson. Inside the method, we have created the getObjectData() method to set the values to the Product object and return that object of the Product class.

In the main() method, we have created an object of the Product class and the ObjectMapper class. After that, we will use the writeValueAsString() method of the ObjectMapper class to covert the Java object into a JSON object.

ObjectToJson.java

When we run the ObjectToJson.java file, we will see the JSON string of the Product class object.

Convert Java object to JSON

Both methods are used in different scenarios. Both are simple, fast and easy to use and understand.






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA