Mark Mark - 1 year ago 104
Java Question

Deserialize JSON array to Map using Jackson

I have JSON structured like:

"id" : "123",
"name" : [ {
"stuff" : [ {
"id" : "234",
"name" : "Bob"
}, {
"id" : "345",
"name" : "Sally"
} ]
} ]

That I want to map to the following data structure:


@JsonSerialize(as = ImmutableMyInterface1.class)
@JsonDeserialize(as = ImmutableMyInterface1.class)
public interface MyInterface1 {
String id();
@JsonDeserialize(using = MyInterface1Deserializer.class)
List<MyInterface2> name();


@JsonSerialize(as = ImmutableMyInterface2.class)
@JsonDeserialize(as = ImmutableMyInterface2.class)
public interface MyInterface2 {
@JsonDeserialize(using = StuffDeserializer.class)
Map<String, MyInterface3> stuff();


@JsonSerialize(as = ImmutableMyInterface3.class)
@JsonDeserialize(as = ImmutableMyInterface3.class)
public interface MyInterface3 {
String id();
String name();

I'm using an ObjectMapper with
to map this JSON to MyInterface1, which should continue down the chain to MyInterface3. This setup was working when I was using a List in MyInterface2, i.e.
List<MyInterface3> name();

However, I want this to be a map instead of a list, ideally with "id" from the inner JSON as the key. This would allow me to get values with the following syntax:

The problem is that when attempting to create a custom StuffDeserializer.class, I'm getting the error:
Can not deserialize instance of$Json out of START_ARRAY token

when trying to do:

public Map<String, MyInterface3> deserialize(JsonParser jsonParser, DeserializationContext ctxt)
throws IOException {

MyInterface2 foo = Unmarshaller.OBJECT_MAPPER.readValue(jsonParser, MyInterface2.class); // error here

I think this is because Jackson is expecting "stuff" to be a List 'cause of the JSON array. What's the best way to deserialize this JSON to a map that uses values from the inner JSON as a key?

Answer Source

I would create a custom JsonDeserializer to map id and name into a map:

public class StringHashMapValueDeserializer extends JsonDeserializer<HashMap<String, String>>{

    public HashMap<String, String> deserialize(JsonParser parser, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {
        HashMap<String, String> ret = new HashMap<String, String>();

        ObjectCodec codec = parser.getCodec();
        TreeNode node = codec.readTree(parser);

        if (node.isArray()){
            for (JsonNode n : (ArrayNode)node){
                JsonNode id = n.get("id");
                if (id != null){
                    JsonNode name = n.get("name");
                    ret.put(id.asText(), name.asText());
        return ret;

And then I would create simple beans with annotating stuff property with the deserializer:

public class Name {

    @JsonDeserialize(using = StringHashMapValueDeserializer.class)
    Map<String, String> stuff;

    public String toString() {
        return "Name [stuff=" + stuff + "]";

Outer type:

public class OuterType {

    String id;
    List<Name> name;

    public String toString() {
        return "OuterType [id=" + id + ", stuff=" + name + "]";


ObjectMapper mapper = new ObjectMapper();

OuterType response;
response = mapper.readValue(json, OuterType.class);


console output:

OuterType [id=123, stuff=[Name [stuff={234=Bob, 345=Sally}]]]

Hope it helps.