Burak Aktas Software Engineer

Convert objects to/from JSON by Jackson example

We will see a simple example about converting objects to/from JSON objects in Java by Jackson. First, add maven dependency for jackson.


And two typical Person and Address classes.


public class Person {

    private String name;
    private String lastName;
    private int age;
    private List<Address> addressList;

    public Person(@JsonProperty("name") String name,
                  @JsonProperty("lastName") String lastName,
                  @JsonProperty("age") int age,
                  @JsonProperty("addressList") List<Address> addressList) {
        this.name = name;
        this.lastName = lastName;
        this.age = age;
        this.addressList = addressList;

    // getters and setters

    public String toString(){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("name: ")
                .append("lastName: ")
                .append("age: ")

        for (Address address: this.addressList) {

        return stringBuilder.toString();


public class Address {

    private int zipcode;
    private String street;

    public Address(@JsonProperty("zipcode") int zipcode,
                   @JsonProperty("street") String street) {
        this.zipcode = zipcode;
        this.street = street;

    // getters and setters

    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("zipcode: ")
                .append("street: ")

        return stringBuilder.toString();


  • @JsonCreator annotation is used for constructors or static factory methods to construct instances from Json. This is called property base creators. Property base creators can have one more or more parameters which all of them have to annotated by @JsonProperty annotations.
  • @JsonProperty annotation is used to bind data by a given name.

1.Object to JSON

public class JacksonExample {

    public static void main(String[] args) throws JsonProcessingException {
        Address homeAddress = new Address(12345, "Stenhammer Drive");
        Address workAddress = new Address(7986, "Market Street");
        List<Address> addressList = new ArrayList<>();
        Person person = new Person("Sawyer", "Bootstrapper", 23, addressList);

        // object mapper is the main object to marshall (write) data into JSON
        ObjectMapper objectMapper = new ObjectMapper();
        String value = objectMapper.writeValueAsString(person);

Output will be;

         "street":"Stenhammer Drive"
         "street":"Market Street"

2.JSON to Object

public class JacksonExample {

    private static String jsonValue = "{\"name\":\"Sawyer\",\"lastName\":\"Bootstrapper\",\"age\":23,\"addressList\":[{\"zipcode\":12345,\"street\":" +
            "\"Stenhammer Drive\"},{\"zipcode\":7986,\"street\":\"Market Street\"}]}";

    public static void main(String[] args) throws IOException {
        Person personValue = objectMapper.readValue(jsonValue, Person.class);

Output will be;

name: Sawyer
lastName: Bootstrapper
age: 23
zipcode: 12345
street: Stenhammer Drive
zipcode: 7986
street: Market Street