UnMarshaling Json file

This post explains unmarshalling a object information stored in json format to java object . This can be achieved with the help of ObjectMapper provided by jackson databind package. To explain with an example we will use the below pojo objects

User

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) {
        this.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) {
        this.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;
    }
    
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append(name).append(",");
        sb.append(ssn).append(",");
        sb.append(sex).append(",");
        sb.append(age).append(",");
        
        for(PhoneNumber phoneNumber : phoneNumbers) {
            sb.append(phoneNumber).append(",");
        }
        sb.append("}");
        return sb.toString();
    }
}

Name

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;
    }
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append(firstName).append(",");
        sb.append(lastName);
        sb.append("}");
        
        return sb.toString();
    }
}

PhoneNumber

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;
    }
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append(areaCode).append("-");
        sb.append(part1).append("-");
        sb.append(part2);
        sb.append("}");
        
        return sb.toString();
    }
}

user.json

{
    "name":{
            "firstName":"firstName1",
            "lastName":"lastName1"
        },
    "ssn":"ssn1",
    "sex":"M",
    "age":28,
    "phoneNumbers":[
        {
            "areaCode":"111",
            "part1":"222",
            "part2":"3333"
        },
        {
            "areaCode":"444",
            "part1":"555",
            "part2":"6666"
        }
    ]
}

Now the main code which does the unmarshalling is as shown below

Main Code

import java.io.File;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class UnMarshallingDemo {
    public static void main(String[] args) throws JsonMappingException, JsonGenerationException, IOException {
        File file = new File("user.json");
        
        ObjectMapper objectMapper = new ObjectMapper();
        User user = objectMapper.readValue(file, User.class);
        System.out.println(user);
    }
}

As shown in the main code, we create an instance of ObjectMapper
ObjectMapper objectMapper = new ObjectMapper();

Then we instruct the objectMapper to read a json content stored in a file (represented by file object), create an object of User class and set the values using appropriate setters.
objectMapper.readValue(file, User.class);

Output

{{firstName1,lastName1},ssn1,M,28,{111-222-3333},{444-555-6666},}

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s