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 objectMapper
configuration:
new ObjectMapper().configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
Is there a way to deserialize duplicate keys to a list using Jackson?