JSON processing using Jackson Java JSON Processor

JSON(Javascript Object Notation) is becoming more popular data exchange format.
While developing Web applications using Javascript frameworks like YUI, ExtJS, DOJO etc
we can use either XML or JSON to exchange the data between the client and server.
Normally we get the response from the server in terms of java objects. Then in Servlets or Action classes we need to build the JSON from objects and send back to client.
To build the JSON response from Java object we can use Jackson Java JSON Processor which very much easy to use.

Let us see how we can use Jackson Java JSON Processor to convert a java object into JSON and vice versa.

We can download the jackson-all-1.6.4.jar from http://jackson.codehaus.org.

The key class which does the marshalling and unmarshalling is org.codehaus.jackson.map.ObjectMapper.

Let us create a User javabean as follows:

package com.sivalabs.json;
import java.util.Date;

public class User
private String userId;
private UserName userName;
private Date dob;

public String toString()
return "User [dob=" + dob + ", userId=" + userId + ", userName="+ userName + "]";
//setters and getters


package com.sivalabs.json;
public class UserName
private String firstname;
private String middlename;
private String lastname;

public String toString()
return "UserName [firstname=" + firstname + ", lastname=" + lastname+ ", middlename=" + middlename + "]";
//setters and getters

Now let us create an instance of User and marshall it into JSON:

ObjectMapper mapper = new ObjectMapper();

UserName userName = new UserName();

User user = new User();
user.setDob(new Date());

Writer strWriter = new StringWriter();
mapper.writeValue(strWriter, user);
String userDataJSON = strWriter.toString();

This will print the User data in JSON format as :

Now let us unmarshall the following user data in json format into User Object:


String userDataJSON = "{"userId":"100","userName":{"firstname":"K","middlename":"Siva","lastname":"Prasad"},"dob":1300878089906}";
User userFromJSON = mapper.readValue(userDataJSON, User.class);

This will print the User object as :
User [dob=Wed Mar 23 16:31:29 IST 2011, userId=100, userName=UserName [firstname=K, lastname=Prasad, middlename=Siva]]

The Date value is marshalled as Timestamp which is the default behaviour. If you want you can change the DateFormat as follows:

DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
SerializationConfig serConfig = mapper.getSerializationConfig();
DeserializationConfig deserializationConfig = mapper.getDeserializationConfig();
mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);

Then the User JSON will be :

We can also marshall a Java Object as json into a file as:

mapper.writeValue(new File("user.json"), user);

This will create a file user.json as :

We can build the User object from user,json as:

User user = mapper.readValue(new File("user.json"), User.class);

For more information you can visit http://jackson.codehaus.org .