Mapping Nested Values with Jackson

1. Overview

A typical use case when working with JSON is to perform a transformation from one model into another. For example, we might want to parse a complex, densely nested object graph into a more straightforward model for use in another domain.

In this quick article, we’ll look at how to map nested values with Jackson to flatten out a complex data structure. We’ll deserialize JSON in three different ways:

  • Using@JsonProperty
  • Using JsonNode
  • Using a custom JsonDeserializer

2. Maven Dependency

Let’s first add the following dependency to pom.xml:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.11.1</version>
</dependency>

We can find the latest versions of jackson-databind on Maven Central.

3. JSON Source

Consider the following JSON as the source material for our examples. While the structure is contrived, note that we include properties that are nested two levels deep:

{
    "id": "957c43f2-fa2e-42f9-bf75-6e3d5bb6960a",
    "name": "The Best Product",
    "brand": {
        "id": "9bcd817d-0141-42e6-8f04-e5aaab0980b6",
        "name": "ACME Products",
        "owner": {
            "id": "b21a80b1-0c09-4be3-9ebd-ea3653511c13",
            "name": "Ultimate Corp, Inc."
        }
    }  
}

4. Simplified Domain Model

In a flattened domain model described by the Product class below, we’ll extract brandName, which is nested one level deep within our source JSON. Also, we’ll extract ownerName, which is nested two levels deep and within the nested brand object:

public class Product {

    private String id;
    private String name;
    private String brandName;
    private String ownerName;

    // standard getters and setters
}

5. Mapping With Annotations

To map the nested brandName property, we first need to unpack the nested brand object to a Map and extract the name property. Then to map ownerName, we unpack the nested owner object to a Map and extract its name property.

We can instruct Jackson to unpack the nested property by using a combination of @JsonProperty and some custom logic that we add to our Product class:

public class Product {
    // ...

    @SuppressWarnings("unchecked")
    @JsonProperty("brand")
    private void unpackNested(Map<String,Object> brand) {
        this.brandName = (String)brand.get("name");
        Map<String,String> owner = (Map<String,String>)brand.get("owner");
        this.ownerName = owner.get("name");
    }
}

Our client code can now use an ObjectMapper to transform our source JSON, which exists as the String constant SOURCE_JSON within the test class:

@Test
public void whenUsingAnnotations_thenOk() throws IOException {
    Product product = new ObjectMapper()
      .readerFor(Product.class)
      .readValue(SOURCE_JSON);

    assertEquals(product.getName(), "The Best Product");
    assertEquals(product.getBrandName(), "ACME Products");
    assertEquals(product.getOwnerName(), "Ultimate Corp, Inc.");
}

6. Mapping With JsonNode

Mapping a nested data structure with JsonNode requires a little more work. Here, we use ObjectMapper‘s readTree to parse out the desired fields:

@Test
public void whenUsingJsonNode_thenOk() throws IOException {
    JsonNode productNode = new ObjectMapper().readTree(SOURCE_JSON);

    Product product = new Product();
    product.setId(productNode.get("id").textValue());
    product.setName(productNode.get("name").textValue());
    product.setBrandName(productNode.get("brand")
      .get("name").textValue());
    product.setOwnerName(productNode.get("brand")
      .get("owner").get("name").textValue());

    assertEquals(product.getName(), "The Best Product");
    assertEquals(product.getBrandName(), "ACME Products");
    assertEquals(product.getOwnerName(), "Ultimate Corp, Inc.");
}

7. Mapping With Custom JsonDeserializer

Mapping a nested data structure with a custom JsonDeserializer is identical to the JsonNode approach from an implementation point of view. We first create the JsonDeserializer:

public class ProductDeserializer extends StdDeserializer<Product> {

    public ProductDeserializer() {
        this(null);
    }

    public ProductDeserializer(Class<?> vc) {
        super(vc);
    }

    @Override
    public Product deserialize(JsonParser jp, DeserializationContext ctxt) 
      throws IOException, JsonProcessingException {
 
        JsonNode productNode = jp.getCodec().readTree(jp);
        Product product = new Product();
        product.setId(productNode.get("id").textValue());
        product.setName(productNode.get("name").textValue());
        product.setBrandName(productNode.get("brand")
          .get("name").textValue());
        product.setOwnerName(productNode.get("brand").get("owner")
          .get("name").textValue());		
        return product;
    }
}

7.1. Manual Registration of Deserializer

To manually register our custom deserializer, our client code must add the JsonDeserializer to a Module, register the Module with an ObjectMapper, and call readValue:

@Test
public void whenUsingDeserializerManuallyRegistered_thenOk()
 throws IOException {
 
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    module.addDeserializer(Product.class, new ProductDeserializer());
    mapper.registerModule(module);

    Product product = mapper.readValue(SOURCE_JSON, Product.class);
 
    assertEquals(product.getName(), "The Best Product");
    assertEquals(product.getBrandName(), "ACME Products");
    assertEquals(product.getOwnerName(), "Ultimate Corp, Inc.");
}

7.2. Automatic Registration of Deserializer

As an alternative to the manual registration of the JsonDeserializer, we can register the deserializer directly on the class:

@JsonDeserialize(using = ProductDeserializer.class)
public class Product {
    // ...
}

With this approach, there is no need to register manually. Let’s take a look at our client code using automatic registration:

@Test
public void whenUsingDeserializerAutoRegistered_thenOk()
  throws IOException {
 
    ObjectMapper mapper = new ObjectMapper();
    Product product = mapper.readValue(SOURCE_JSON, Product.class);

    assertEquals(product.getName(), "The Best Product");
    assertEquals(product.getBrandName(), "ACME Products");
    assertEquals(product.getOwnerName(), "Ultimate Corp, Inc.");
}

8. Conclusion

In this tutorial, we demonstrated several ways of using Jackson to parse JSON containing nested values. Have a look at our main Jackson Tutorial page for more examples.

And, as always, code snippets, can be found over on GitHub.

Related posts:

Giới thiệu JDBC Connection Pool
Use Liquibase to Safely Evolve Your Database Schema
Java Program to Implement Dijkstra’s Algorithm using Priority Queue
Java Program to Implement Quick Sort Using Randomization
A Guide to TreeSet in Java
Java Program to Perform Finite State Automaton based Search
Java Program to Use Dynamic Programming to Solve Approximate String Matching
Java Program to Solve any Linear Equation in One Variable
Sort a HashMap in Java
Comparing Objects in Java
Programmatic Transaction Management in Spring
Java Program to Construct an Expression Tree for an Prefix Expression
Using the Not Operator in If Conditions in Java
Hướng dẫn Java Design Pattern – Bridge
Create a Custom Exception in Java
The Registration API becomes RESTful
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Java Program to Implement Ternary Search Algorithm
Prevent Brute Force Authentication Attempts with Spring Security
Debug a JavaMail Program
Spring Security Authentication Provider
Serverless Functions with Spring Cloud Function
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle
Spring Boot Change Context Path
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
Java Program to Implement Network Flow Problem
@Lookup Annotation in Spring
Java Program to Implement Bellman-Ford Algorithm
Java Program to Implement Bucket Sort
Daemon Threads in Java
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Java Program to Describe the Representation of Graph using Incidence Matrix