Getting started with Jackson JSON

For updated post, visit here.

I recently came across a project that required me to get JSON from POJOs. After searching I came across Jackson JSON. In this post I will try to give you brief idea about how to get started with it. I will do it by showing an example which converts POJOs to JSON and vice versa and some variations. So let’s get started with it-

First of all, What is Jackson? (taken from here)

Jackson is a:

  • Streaming (reading, writing)
  • FAST (measured to be faster than any other Java json parser and data binder)
  • Powerful (full data binding for common JDK classes as well as any Java bean class, Collection, Map or Enum)
  • Zero-dependency (does not rely on other packages beyond JDK)
  • Open Source (LGPL or AL)
  • Fully conformant
  • Extremely configurable

JSON processor (JSON parser + JSON generator) written in Java. Beyond basic JSON reading/writing (parsing, generating), it also offers full node-based Tree Model, as well as full OJM (Object/Json Mapper) data binding functionality.

How to include it in the project?

If you are manually adding JARs to your class path then you can get JARs from here. If you adding dependencies to your maven project, add these lines to your pom.xml-

	<!-- Jackson JSON Mapper -->


Assuming that you have created your project, you now need to add following imports –

import org.codehaus.jackson.JsonGenerationException;

To convert POJOs to JSON you first need to make a object of ObjectMapper class. If we have a class named University like this –

public class University {
	private String name;
	private int noOfDepartments;
	private ArrayList<String> departments;
	public String getName() {
		return name;
	public void setName(String name) { = name;
	public int getNoOfDepartments() {
		return noOfDepartments;
	public void setNoOfDepartments(int noOfDepartments) {
		this.noOfDepartments = noOfDepartments;
	public ArrayList<String> getDepartments() {
		return departments;
	public void setDepartments(ArrayList<String> departments) {
		this.departments = departments;

And we want to convert object of University class to JSON

ObjectMapper mapper = new ObjectMapper();
String uniJSON = mapper.writeValueAsString(uni);

where uni is an object of University class. If we want to print the JSON to a file we can do this by-

mapper.writeValue(new File("uni-json.json"), uni);

To ignore some class variables from converting to JSON (for e.g. suppose we don’t want to convert noOfDepartments then we have to add @JsonIgnore annotation before getter method so now, the getter method would now be –

public int getNoOfDepartments() {
		return noOfDepartments;

, , , ,

  1. #1 by anand on December 12, 2012 - 12:11 pm

    Thanks a lot for providing tutorial about json and pojo

  2. #2 by pankaj on May 26, 2013 - 11:59 pm

    Thanks a lot for the basic but conceptual blog!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s