Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize duplicate keys to list using Jackson

I'm trying to deserialize json into object. However, the json has duplicate keys. I cannot change the json and I would like to use Jackson to change duplicate keys to a list.

Here is an example of the json I retrieve:

{
  "myObject": {
    "foo": "bar1",
    "foo": "bar2"
  }
}

And here is what I would like after deserialization:

{
  "myObject": {
    "foo": ["bar1","bar2"]
  }
}

I created my class like so:

public class MyObject {
    private List<String> foo;
    // constructor, getter and setter
}

I tried to use DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY from objectMapper but all it does is taking the last key and add it to the list like this:

{
  "myObject": {
    "foo": ["bar2"]
  }
}

Here is my objectMapperconfiguration:

new ObjectMapper().configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

Is there a way to deserialize duplicate keys to a list using Jackson?

like image 291
JsonMraz Avatar asked Sep 03 '25 06:09

JsonMraz


1 Answers

You need to use com.fasterxml.jackson.annotation.JsonAnySetter annotation:

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class JsonPathApp {

    public static void main(String[] args) throws Exception {
        File jsonFile = new File("./resource/test.json").getAbsoluteFile();

        ObjectMapper mapper = new ObjectMapper();
        Root root = mapper.readValue(jsonFile, Root.class);
        root.getMyObject().getFoos().forEach(System.out::println);
    }
}

class Root {

    private MyObject myObject;

    // getters, setters, toString
}

class MyObject {

    private List<String> foos = new ArrayList<>();

    @JsonAnySetter
    public void manyFoos(String key, String value) {
        foos.add(value);
    }

    // getters, setters, toString
}

On Java side you have a list with values:

bar1
bar2
like image 110
Michał Ziober Avatar answered Sep 04 '25 21:09

Michał Ziober