Saturday, 27 February, 2021 UTC


Summary

In this tutorial, we'll be taking a look at how to convert a JSON object into a Java Object using Jackson, an extremely popular data-binding library for Java.
Specifically, we'll be working with this JSON object:
{
   "name":"David",
   "position":"Software Engineer",
   "skilltree":[
      "Java",
      "Python",
      "JavaScript"
   ],
   "address":{
      "street":"Street",
      "streetNo":"123"
   }
}
Since we're working with an external library, let's add the dependency. If you're using Maven, you can add it with:
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.11.3</version>
</dependency>
Or if you're using Gradle, you can add:
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.11.3'

Creating a Custom Class

With that done, we can create a simple custom class to host our Employee information from the JSON contents:
public class Employee {

    private String name;
    private POSITION position;
    private List<String> skilltree;
    private Address address;

    // Constructors, Getters, Setters, toString()
}
Note: If we want Jackson to be able to automatically bind properties of our class and the properties in the JSON object, they have to have the exact same names. We'll cover how to change this behavior a bit later. Also, you'll want to have an empty constructor for instantiation.
Here, we're using an enum, POSITION, which looks like this:
public enum POSITION {
    MANAGER, SOFTWARE_ENGINEER, CEO
}
And an Address object:
public class Address {
    private String street;
    private String streetNo;

    // Constructors, Getters and Setters, toString()
    
}

Convert JSON Object to Java Object

Jackson's central class is the ObjectMapper. It's the main API for object-related data-binding and you'll use it all the time with Jackson.
To convert a JSON object into a Java object, you'll use the readValue() method of the ObjectMapper instance, which deserializes it into the provided class reference:
String json = "{ \"name\":\"David\", \"position\":\"SOFTWARE_ENGINEER\", \"skilltree\":[ \"Java\", \"Python\", \"JavaScript\" ], \"address\":{ \"street\":\"Street\", \"streetNo\":\"123\" } }";

// ObjectMapper instantiation
ObjectMapper objectMapper = new ObjectMapper();

// Deserialization into the `Employee` class
Employee employee = objectMapper.readValue(json, Employee.class);

// Print information
System.out.println(employee);
Running this code will result in:
Employee{name='David', position=SOFTWARE_ENGINEER, skillTree=[Java, Python, JavaScript], address=Address{street='Street', streetNo='123'}}
Another way to create a let Jackson know into which class it should deserialize into is to use a TypeReference:
Employee employee = objectMapper.readValue(json, new TypeReference<Employee>(){});
Printing this employee instance will also result in:
Employee{name='David', position=SOFTWARE_ENGINEER, skillTree=[Java, Python, JavaScript], address=Address{street='Street', streetNo='123'}}
Both of these construct the given object and call the exact same deserialization process. So the only difference between these two calls is whether you're making a static or dynamic reference to the type.