How to convert Java POJO to JSON using Jackson

Priya Salvi
3 min readApr 28, 2024

--

Introduction to JSON:

JSON, which stands for JavaScript Object Notation, is a lightweight data interchange format commonly used for transmitting data between a server and a web application, or between different parts of an application. It is language-independent, human-readable, and easy for both humans and machines to understand. JSON data is organized as key-value pairs, where each key is a string and each value can be a string, number, boolean, array, or another JSON object.

Here’s an example of JSON data representing information about a person:

{
"name": "John Doe",
"age": 30,
"isStudent": false,
"address": {
"street": "123 Main St",
"city": "New York",
"zipcode": "10001"
},
"phoneNumbers": ["123-456-7890", "987-654-3210"]
}

Why should we convert Java POJO to JSON?

Converting Java POJO to JSON is essential for serializing Java objects into a format that can be easily transmitted, stored, or processed. It’s commonly used for:

  1. Data Serialization: Representing Java objects in a platform-independent and human-readable format.
  2. Interoperability: Enabling seamless communication and data exchange with systems in other languages.
  3. API Responses: Returning structured data in JSON format from Java web services or APIs.
  4. Data Persistence: Storing Java objects as JSON documents in databases or configuration files.

For example, you can convert a Person object to JSON using libraries like Jackson or Gson, facilitating data exchange between Java and other systems.

How to convert Java POJO to JSON using Jackson?

To convert a Java POJO to JSON using Jackson, you can follow these steps:

  1. Add the Jackson library to your project dependencies.
  2. Create an instance of the ObjectMapper class from Jackson.
  3. Use the writeValueAsString() method of the ObjectMapper instance to serialize the Java object to JSON format.

Here’s how you can convert the Person class to JSON using Jackson:

import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
public static void main(String[] args) throws Exception {
// Create a Person object
Person person = new Person("John Doe", 30, "123 Main St");

// Convert Person object to JSON
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(person);

// Print JSON
System.out.println(json);
}
}

In this example:

  • We create an instance of the Person class representing a person with name, age, and address attributes.
  • We create an instance of the ObjectMapper class from Jackson, which is responsible for converting Java objects to JSON and vice versa.
  • We use the writeValueAsString() method of the ObjectMapper instance to serialize the Person object to a JSON string.
  • Finally, we print the JSON string representing the Person object.

Ensure that you have the Jackson library added to your project dependencies. You can add it using a build tool like Maven or Gradle. Here’s a Maven dependency for Jackson:

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.4</version>
</dependency>

Replace the version number with the latest version available. Once you have added the dependency, you can use the ObjectMapper class from Jackson to convert Java POJOs to JSON and vice versa in your Java application.

The response of the above code, after converting the Person object to JSON using Jackson, would be a JSON string representing the attributes of the Person object.

{
"name":"John Doe",
"age":30,
"address":"123 Main St"
}

This JSON string represents the Person object in a structured format that can be easily transmitted, stored, or processed by other systems. Each attribute of the Person object is represented as a key-value pair in the JSON object.

--

--

Priya Salvi

Software Engineer | Oracle Certified Java Associate | Neophile | Bibliophile