Convert Java Object To Or From JSON Using Jackson API

Posted By : Sanjay Saini | 25-Feb-2018

Hi Guys,

In this blog, I will explain to you how we use Jackson API with Java objects. Many times we will need to convert JAVA OBJECTS into JSON OBJECT and JSON OBJECT to JAVA OBJECT.

What is JACKSON?

JACKSON is Java-based API, it is useful to convert Java object to JSON and JSON to Java Object.

Benefits of Jackson

Simple & Easy to use − JACKSON API provides lots of easy to use inbuilt methods.

Default Mapping Structure − Jackson API provides inbuilt mapping for many of the objects to be serialized.

Performance − Jackson API is faster than Other API, need less memory area and is good for the large object.

No Dependency − Jackson library not depend on other library other than JDK.

Open Source − Jackson API is Free to use and open Source API.

Process JSON using Jackson

Jackson provides three different ways to process JSON −

Streaming API - It perform operations on JSON data as inconsistent events. 
Json Parser parse the data and read it, while Json Generator writes the data.This is most strong approach between all.It is same to Stax parser for XML.

Tree Model −It convert an in-memory tree representation of the JSON data. ObjectMapper create tree of JsonNode nodes. It is most resilient approach. It is similar to DOM parser for XML.

Data Binding −It can change JSON to and from Plain Old Java Object (POJO). ObjectMapper perform both read and write operations on JSON. Data binding is of two types −

Simple Data Binding − It changes JSON to and from Java objects.

Full Data Binding − It changes JSON to and from any Java DataType.

Pom.xml

<dependency>
<groupid>com.fasterxml.jackson.core</groupid>
<artifactid>jackson-databind</artifactid>
<version>2.8.9</version>
</dependency>

Create an Employee POJO Class

package com.sanjay;

public class Employee {

	private String name;
	private int age;
	private int id;

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	 public String toString(){
		return "Id : "+id +" | name : "+name +" | age : "+age;
		 
	 }
}

Convert JAVA Object to JSON

package com.sanjay;

import java.io.File;
import java.io.IOException;

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

public class JavaToJackson {

	public static void main(String[] args) {

		Employee employee = new Employee();
		employee.setName("Sanjay");
		employee.setAge(27);
		employee.setId(1);

		try {
			ObjectMapper objectMapper = new ObjectMapper();
			objectMapper.writeValue(new File("F:\\employee.json"), employee);

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

			//Convert object to JSON string and pretty print
			jsonString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(employee);
			System.out.println(jsonString);


		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

Convert JSON To JAVA Object


package com.sanjay;

import java.io.File;
import java.io.IOException;

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

public class JacksonToJava {

	public static void main(String[] args) {

		ObjectMapper objectMapper = new ObjectMapper();
		try {

			// Convert JSON string from file to Object
			Employee employee = objectMapper.readValue(new File("F:\\employee.json"), Employee.class);
			System.out.println(employee);

			// Convert JSON string to Object
			String jsonString = "{\"id\":1,\"age\":27,\"name\":\"sanjay\"}";
			employee = objectMapper.readValue(jsonString, Employee.class);
			System.out.println(employee);

		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

About Author

Author Image
Sanjay Saini

Sanjay has been working on web application development using frameworks like Java, groovy and grails. He loves listening to music , playing games and going out with friends in free time.

Request for Proposal

Name is required

Comment is required

Sending message..