Convert JSON to Java Object and Java Object to JSON in Java using Jackson

In this article, we will see how to convert JSON to Java Object and Java Object to JSON in Java using Jackson API.
Java Jackson api

Jackson is one of the popular libraries for processing JSON.

We can use Jackson to convert Java objects to JSON string and vice versa.

 

Maven dependencies for Jackson

For a simple Maven project setup, you may refer this article :

Maven Basics

For using Jackson, add the following dependency in your pom.xml:

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

 

Parsing JSON String to Java Object using Jackson

 

The following example illustrates how to parse a JSON into an Employee object.

Employee.java

package com.topjavatutorial.app;

public class Employee {
  private String name;
  private long employeeId;
  private int age;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public long getEmployeeId() {
    return employeeId;
  }

  public void setEmployeeId(long employeeId) {
    this.employeeId = employeeId;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }
  
   @Override
      public String toString() {
          StringBuilder sb = new StringBuilder();
          sb.append("\n----- Employee Information-----\n");
          sb.append("ID: " + getEmployeeId() + "\n");
          sb.append("Name: " + getName() + "\n");
          sb.append("Age: " + getAge() + "\n");
          sb.append("*****************************");
          return sb.toString();
   }

}


 

App.java

package com.topjavatutorial.app;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class App {
  public static void main(String[] args) {
    String jsonString = "{\"name\":\"John Doe\",\"employeeId\":\"101\",\"age\":\"25\"}";
    try {
      parseJSON(jsonString);
    } catch (JsonParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (JsonMappingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  private static void parseJSON(String str) throws JsonParseException,
      JsonMappingException, IOException {

    ObjectMapper mapper = new ObjectMapper();
    Employee emp = mapper.readValue(str, Employee.class);
    System.out.println(emp);
  }
}


 

Output



----- Employee Information-----
ID: 101
Name: John Doe
Age: 25
*****************************

 

Create JSON String from Java Object using Jackson

 
The Jackson library can also be used to create JSON from a domain object.

Here is an example :

package com.topjavatutorial.app;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class App {
  public static void main(String[] args) {
    Employee emp = new Employee();
    emp.setName("John Doe");
    emp.setAge(25);
    emp.setEmployeeId(101);
    try {
      writeJSON(emp);
    } catch (JsonGenerationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (JsonMappingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }

  private static void writeJSON(Employee emp) throws JsonGenerationException,
      JsonMappingException, IOException {
    ObjectMapper mapper = new ObjectMapper();

    System.out.print("Employee object as JSON = ");
    mapper.writeValue(System.out, emp);
  }
}


 

Output


Employee object as JSON = {"name":"John Doe","employeeId":101,"age":25}

 

We can also use the following for JSON processing:

Convert Java Object to JSON and vice versa using JSON.simple

 

Further Reading

 
Java API for JSON Processing
How to convert String to XML ans XML to String in Java
Jackson GitHub page
XML vs JSON

 

© 2016 – 2018, https:. All rights reserved. On republishing this post, you must provide link to original post

One comment

  1. Hello, please:

    How may I accomplish the same with BOTH a Model bean and a managed bean that connects the MODEL to a remote MySQL data source?

    http://stackoverflow.com/questions/41142802/jsf-managed-bean-accessing-mysql-remote-database-how-to-create-json-array-to-f

    Any suggestions greatly appreciated.

    Jon

Leave a Reply.. code can be added in <code> </code> tags

%d bloggers like this: