JACKSON – JSON library for Java

In this tutorial lets see how to convert a Java object to/from JSON using Jackson in Eclipse.

What Jar files are needed ?

Basically we need three jar files; jackson-core, jackson-annotations & jackson-databind.

For this tutorial i am downloading

1. jackson-core-2.5.5.jar
2. jackson-annotations-2.6.0.jar
3. jackson-databind-2.5.5.jar

From the urls mentioned below.

https://github.com/FasterXML/jackson-core/wiki/
https://github.com/FasterXML/jackson-annotations/wiki
https://github.com/FasterXML/jackson-databind/wiki

Just search for the version you need, right click and “Save Link As..” to save the jar to your local.
An example image is provided for downloading jackson-databind-2.5.5.jar

databind

How to convert a java object to JSON ?

To start with, create a java project in eclipse and add the above mentioned 3 jars into the project classpath.

Then lets create a simple java object (UserDO) with three properties say name, id and date for birth and populate them with values as shown below.

Create 2 userDO objects and add them to an ArrayList.

//Creating UserDO1.
UserDO userDO1 = new UserDO();
userDO1.setId(111);
userDO1.setName("Dinosaur");
userDO1.setDob("200 million BCE");

//Creating UserDO2.
UserDO userDO2 = new UserDO();
userDO2.setId(222);
userDO2.setName("Tyrannosaurus");
userDO2.setDob("68 million BCE");

//Setting both UserDO1 and UserDO2 into an ArrayList.
ArrayList<UserDO> userDOList = new ArrayList();
userDOList.add(userDO1);
userDOList.add(userDO2);

Now lets convert the userDO1 and the ArrayList object to a JSON String. First we need to create an ObjectMapper object. Then call writeValueAsString method by passing the object (example: userDO1) that needs to be converted to JSON.

//Creating jackson Objectmapper object.
ObjectMapper mapper = new ObjectMapper();
//Converting userDO to JSON String and printing.
String userDO1_Json = mapper.writeValueAsString(userDO1);
System.out.println("JSON String of UserDO1 :\n" + userDO1_Json);

//Converting userDO arrayList to JSON String.
String userDOArrayList_Json = mapper.writeValueAsString(userDOList);
System.out.println("JSON String of ArrayList :\n" + userDOArrayList_Json);

The writeValueAsString method from ObjectMapper class at line 4 & 8 does the trick. The output is provided below.

JSON String of UserDO1 :
{"name":"Dinosaur","id":111,"dob":"200 million BCE"}
JSON String of ArrayList :
[{"name":"Dinosaur","id":111,"dob":"200 million BCE"},{"name":"Tyrannosaurus","id":222,"dob":"68 million BCE"}]

How to convert a JSON into a java object?

First we need to create an ObjectMapper object and call the readValue method on it.
We need to passing the JSON string and specify the type of java object which needs to be created from JSON;

For example UserDO.class needs to be passed into readValue for the first JSON: userDO1_Json

userDO1_Json = {"name":"Dinosaur","id":111,"dob":"200 million BCE"}
ObjectMapper mapper = new ObjectMapper();
//Converting UserDO1 JSON back to UserDO
UserDO UserDO1_FromJSON = mapper.readValue(userDO1_Json, UserDO.class);
System.out.println("\n\n"+ UserDO1_FromJSON);

Output:

Printing UserDO: 
name = Dinosaur
id = 111
dob = 200 million BCE

A new TypeReference<ArrayList> object needs to be passed into readValue method for the Second JSON userDOArrayList_Json to convert it into an ArrayList.

userDOArrayList_Json=[{"name":"Dinosaur","id":111,"dob":"200 million BCE"},{"name":"Tyrannosaurus","id":222,"dob":"68 million BCE"}]
ObjectMapper mapper = new ObjectMapper();
//Converting userDOArrayList JSON back to Arraylist of UserDOs.
ArrayList&lt;UserDO&gt; userDOArrayList_fromJSON = mapper.readValue(userDOArrayList_Json, new TypeReference&lt;ArrayList&lt;UserDO&gt;&gt;() {});
System.out.println("\nPrint UserDOArrayList_FromJSON :\n" + userDOArrayList_fromJSON);

Output:

Print UserDOArrayList_FromJSON :
[Printing UserDO: 
name = Dinosaur
id = 111
dob = 200 million BCE, Printing UserDO: 
name = Tyrannosaurus
id = 222
dob = 68 million BCE]
The full code used in this example is provided below.
1. UserDO.java

package com.jsession4d;

import java.util.ArrayList;

public class UserDO {

private String name;
private long id;
private String dob;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}

public String toString(){
return "Printing UserDO: \nname = " + name + "\n" + "id = " + id + "\n" + "dob = " + dob;
}

}

2. RunJackson.java

package com.jsession4d;

import java.util.ArrayList;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jsession4d.UserDO;

public class RunJackson {

public static void main(String[] args) {
// TODO Auto-generated method stub

try{
//Creating UserDO1.
UserDO userDO1 = new UserDO();
userDO1.setId(111);
userDO1.setName("Dinosaur");
userDO1.setDob("200 million BCE");

//Creating UserDO2.
UserDO userDO2 = new UserDO();
userDO2.setId(222);
userDO2.setName("Tyrannosaurus");
userDO2.setDob("68 million BCE");

//Setting both UserDO1 and UserDO2 into an ArrayList.
ArrayList<UserDO> userDOList = new ArrayList();
userDOList.add(userDO1);
userDOList.add(userDO2);

//Creating jackson Objectmapper object.
ObjectMapper mapper = new ObjectMapper();
//Converting userDO to JSON String and printing.
String userDO1_Json = mapper.writeValueAsString(userDO1);
System.out.println("JSON String of UserDO1 :\n" + userDO1_Json);

//Converting userDO arrayList to JSON String.
String userDOArrayList_Json = mapper.writeValueAsString(userDOList);
System.out.println("JSON String of ArrayList :\n" + userDOArrayList_Json);

//Converting UserDO1 JSON back to UserDO
UserDO UserDO1_FromJSON = mapper.readValue(userDO1_Json, UserDO.class);
System.out.println("\n\n"+ UserDO1_FromJSON);

//Converting userDOArrayList JSON back to Arraylist of UserDOs.
ArrayList<UserDO> userDOArrayList_fromJSON = mapper.readValue(userDOArrayList_Json, new TypeReference<ArrayList<UserDO>>() {});
System.out.println("\nPrint UserDOArrayList_FromJSON :\n" + userDOArrayList_fromJSON);

}catch(Exception e){
e.printStackTrace();
}

}

}
[/code]

3. Output

JSON String of UserDO1 :
{"name":"Dinosaur","id":111,"dob":"200 million BCE"}
JSON String of ArrayList :
[{"name":"Dinosaur","id":111,"dob":"200 million BCE"},{"name":"Tyrannosaurus","id":222,"dob":"68 million BCE"}]


Printing UserDO: 
name = Dinosaur
id = 111
dob = 200 million BCE

Print UserDOArrayList_FromJSON :
[Printing UserDO: 
name = Dinosaur
id = 111
dob = 200 million BCE, Printing UserDO: 
name = Tyrannosaurus
id = 222
dob = 68 million BCE]

The same code, works perfectly well for complex java objects too.

Lets make our UserDO java object little more complex by adding a nested object or an object inside the UserDO object and an ArrayList.
Navigate to next page to see it in action.

%d bloggers like this: