Jackson Java API : Convert JSON to Object and Object to JSON

In this blog I will explain you what is Jackson, how it is used for mapping JSON to Object and manipulating JSON.

Brief introduction of JSON :JSON stand for Javascript Object Notation. It is a lightweight data-interchange format. It is easy for humans to read and write. In other words we can say It is platform independent language through which two application can communicate with each other.

Jackson Java API: It is a very popular Java api used for converting JSON to Object and Object to JSON.

How we will use it :


Suppose we have a following simple JSON format

and We have three Classes User, Profile and Address




Now we will see how easily we can convert this JSON to an object.

Similarly do the job here for converting JSON to Map

Even though we can update JSON on the fly

One thing to note here that JSON keys name has to be same as the object’s propertyName. But what happen if they are different. Jackson comes with bunch of Annotation and we can fix it by using Jackson Annotation.

For example suppose your json is

In above JSON I have replaced fistName key with first and lastName key with last. To automatically bind first with firstName you have to use @JsonProperty anotation on the firstName and lastName.

There are many other useful annotation like @JsonUnwrapped(prefix = “profile.”) used for binding the key start with some prefix, @JsonFormat(shape=JsonFormat.Shape.STRING, pattern=”yyyy-MM-dd”) is used for mapping date field, @JsonIgnore used for ignoring a property etc.