Achieving data binding with object model

This post explains how to achieve mapping of json data represented by an object model to particular java object.

Here object model is tree like data structure which reads and stores the entire json content in memory.

The difference between this post and other posts in Binding section is that latter posts explains marshalling and unmarshalling of json data without storing them completely in memory.

Below is an example



User Class

package package10;

public class User {
    private Name name;
    private String ssn;
    private String sex;
    private int age;
    private PhoneNumber[] phoneNumbers;
    public Name getName() {
        return name;
    public void setName(Name name) { = name;
    public String getSsn() {
        return ssn;
    public void setSsn(String ssn) {
        this.ssn = ssn;
    public String getSex() {
        return sex;
    public void setSex(String sex) { = sex;
    public int getAge() {
        return age;
    public void setAge(int age) {
        this.age = age;
    public PhoneNumber[] getPhoneNumbers() {
        return phoneNumbers;
    public void setPhoneNumbers(PhoneNumber[] phoneNumbers) {
        this.phoneNumbers = phoneNumbers;
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for(PhoneNumber phoneNumber : phoneNumbers) {
        return sb.toString();

Name Class

package package10;

public class Name {
    private String firstName;
    private String lastName;
    public String getFirstName() {
        return firstName;
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    public String getLastName() {
        return lastName;
    public void setLastName(String lastName) {
        this.lastName = lastName;
    public String toString() {
        StringBuilder sb = new StringBuilder();
        return sb.toString();

PhoneNumber Class

package package10;

public class PhoneNumber {
    private String areaCode;
    private String part1;
    private String part2;
    public String getAreaCode() {
        return areaCode;
    public void setAreaCode(String areaCode) {
        this.areaCode = areaCode;
    public String getPart1() {
        return part1;
    public void setPart1(String part1) {
        this.part1 = part1;
    public String getPart2() {
        return part2;
    public void setPart2(String part2) {
        this.part2 = part2;
    public String toString() {
        StringBuilder sb = new StringBuilder();
        return sb.toString();

Main code

1  package package10;
3  import;
4  import;
6  import com.fasterxml.jackson.core.JsonProcessingException;
7  import com.fasterxml.jackson.databind.JsonNode;
8  import com.fasterxml.jackson.databind.ObjectMapper;
10 public class TreeModelDemo3 {
11  public static void main(String[] args) throws JsonProcessingException, IOException {
12      ObjectMapper objectMapper = new ObjectMapper();
13      File file = new File("user.json");
14      JsonNode jsonNode = objectMapper.readTree(file);
15      User user = objectMapper.treeToValue(jsonNode, User.class);
16      System.out.println(user);
17  }
18 }


The user.json file contains serialized data of a particular User class’s object.

At line 14 we read the json file content into the object jsonNode.

Then we mapped it to another User instance by calling treeToValue method in ObjectMapper. Refer to line 15.

treeToValue method takes two agruments
1) the object model
2) Class instance of the class to be mapped.




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