Jackson API - Converting POJOs to JSON example

Posted on March 24, 2018


Jackson-databind API provides the general purpose functionally for binding JSON data to Plain Old Java Object (POJO) and vice versa.  It is built on the top of the Jackson Streaming API i.e. jackson-core and uses the jackson-annotation for configuration.

The ObjectMapper class provides the functionality for reading and writing JSON, either to or from basic POJOs.

The following examples demonstrate how to use the ObjectMapper class to convert the POJOs to JSON and vice versa.

JAR dependencies

To use Jackson API’s data binding functionality, put the jackson-databind, jackson-core and jackson-annotation library into your classpath.

If you are using Maven, then add the following dependency in your pom.xml file.

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

Converting POJOs to JSON content

Here is an example of creating JSON from Java object using the ObjectMapper’s writeValueXXX() methods.

Employee.java

package com.boraji.tutorial.jackson;

public class Employee {
   private Long id;
   private String name;
   private Integer salary;
   private String[] skills;
   private Address address;
   
   //Getter and setter methods
}

Address.java

package com.boraji.tutorial.jackson;

public class Address {
   private Long id;
   private String city;
   private String country;
   private String pin;

   //Getter and Setter methods
   
}

JacksonPojoToJson.java

package com.boraji.tutorial.jackson;

import java.io.FileOutputStream;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JacksonPojoToJson {
   public static void main(String[] args) throws Exception {

      // Create ObjectMapper
      ObjectMapper mapper = new ObjectMapper();
      mapper.enable(SerializationFeature.INDENT_OUTPUT);

      Employee employee = new Employee();
      employee.setId(2005l);
      employee.setName("John Doe");
      employee.setSalary(40000);
      employee.setSkills(new String[] { "Java", "SQL", "jQuery" });

      Address address = new Address();
      address.setId(100L);
      address.setCity("Delhi");
      address.setCountry("India");
      address.setPin("231245");

      employee.setAddress(address);

      // Convert object to JSON string
      String json = mapper.writeValueAsString(employee);
      System.out.println(json);

      // Save JSON string to file
      FileOutputStream fileOutputStream = new FileOutputStream("employee.json");
      mapper.writeValue(fileOutputStream, employee);
      fileOutputStream.close();
   }
}

Output

{
  "id" : 2005,
  "name" : "John Doe",
  "salary" : 40000,
  "skills" : [ "Java", "SQL", "jQuery" ],
  "address" : {
    "id" : 100,
    "city" : "Delhi",
    "country" : "India",
    "pin" : "231245"
  }
}

 

Converting JSON content to  POJOs

Here is an example of converting JSON to Java object using the ObjectMapper’s readValue() methods. 

JacksonJsonToPojo.java

package com.boraji.tutorial.jackson;

import java.io.FileInputStream;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonJsonToPojo {
   public static void main(String[] args) throws Exception {
      ObjectMapper mapper = new ObjectMapper();

      // Read JSON file and convert to java object
      FileInputStream fileInputStream = new FileInputStream("employee.json");
      Employee employee = mapper.readValue(fileInputStream, Employee.class);
      fileInputStream.close();

      // Print
      System.out.println("Emplyoee ID : " + employee.getId());
      System.out.println("Emplyoee Name : " + employee.getName());
      System.out.println("Emplyoee Salary : " + employee.getSalary());
      System.out.println("Emplyoee Skills : ");
      for (String skill : employee.getSkills()) {
         System.out.println("\t" + skill);
      }
      
      Address address=employee.getAddress();
      System.out.println("Address ID : "+address.getId());
      System.out.println("City : "+address.getCity());
      System.out.println("Country : "+address.getCountry());
      System.out.println("Pin : "+address.getPin());
      
   }
}

Output

Emplyoee ID : 2005
Emplyoee Name : John Doe
Emplyoee Salary : 40000
Emplyoee Skills : 
	Java
	SQL
	jQuery
Address ID : 100
City : Delhi
Country : India
Pin : 231245