How to Convert JSON Object to Java Object with Jackson

How to Convert JSON Object to Java Object with Jackson

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:

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

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.

Last Updated: February 26th, 2021
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Want a remote job?

    Prepping for an interview?

    • Improve your skills by solving one coding problem every day
    • Get the solutions the next morning via email
    • Practice on actual problems asked by top companies, like:
     
     
     

    Better understand your data with visualizations

    With over 330+ pages, you'll learn the ins and outs of visualizing data in Python with popular libraries like Matplotlib, Seaborn, Bokeh, and more.

    © 2013-2021 Stack Abuse. All rights reserved.