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 :
Example:
Suppose we have a following simple JSON format
1 2 3 4 5 6 7 8 9 10 11 12 |
{"id": 12, "username": "neeraj1986", "email": "neeraj@jellyfishtechnologies.com", "profile": { "firstName": "Neeraj", "lastName": "Bhatt", "address": { "city": "Noida", "country": "India" } } } |
and We have three Classes User, Profile and Address
User.java
1 2 3 4 5 6 |
public class User { private int id; private String username; private Profile profile //and their getter and setter } |
Profile.java
1 2 3 4 5 6 |
public class Profile { private String firstName; private String lastName; privae Address address //and their getter setter } |
Address.java
1 2 3 4 5 |
public class Address { private String city; private String country; //and their getter setter } |
Now we will see how easily we can convert this JSON to an object.
1 2 3 4 |
ObjectMapper mapper = new ObjectMapper(); File file=new File("user.txt"); // try to read json from the file User user=mapper.readValue(file, Employee.class); System.out.println("==============="+user); |
Similarly do the job here for converting JSON to Map
1 2 |
HashMap map=mapper.readValue(file, HashMap.class); System.out.println("============Map============="+map); |
Even though we can update JSON on the fly
1 2 3 4 5 6 7 |
JsonNode jsonNode= mapper.readTree(file); System.out.println("================id======"+jsonNode.get("id")); //updating json data ((ObjectNode)jsonNode).put("role", "Admin"); System.out.println("==============="+jsonNode); ((ObjectNode)jsonNode).remove("role"); System.out.println("==============="+jsonNode); |
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
1 2 3 4 5 6 7 8 9 10 11 12 |
{"id": 12, "username": "neeraj1986", "email": "neeraj@jellyfishtechnologies.com", "profile": { "first": "Neeraj", "last": "Bhatt", "address": { "city": "Noida", "country": "India" } } } |
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.
1 2 3 4 5 6 7 8 |
public class Profile { @JsonProperty("first") private String firstName; @JsonProperty("last") private String lastName; privae Address address //and their getter setter } |
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.
Recent Comments