Home > Net >  Serialize Json with List<Map..> into custom object using Jackson
Serialize Json with List<Map..> into custom object using Jackson

Time:10-06

I have a custom object that has a handful of properties and even has other objects. It also has a List<Map<String, String>> that I am trying to deserialize from json using Jackson and the Object mapper using only annotations.

My Custom object looks like this,

import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.MapSerializer;
import java.util.Date;
import java.util.List;
import java.util.Map;

@JsonInclude(JsonInclude.Include.ALWAYS)
@JsonPropertyOrder({
        "CustomId",
        "Attributes"
})

@Generated("jsonschema2pojo")
public class MyCustomObject {

    public MyCustomObject(){
    }

    @JsonProperty("CustomId")
    private String customId;

    @JsonProperty("Attributes")
    private List<Map<String, String>> attributes;

    @JsonProperty("CustomId")
    public String getCustomId() {
        return customId;
    }

    @JsonProperty("CustomId")
    public void setCustomId(String customId) {
        this.customId = customId;
    }

    @JsonSerialize(keyUsing = MapSerializer.class)
    @JsonProperty("Attributes")
    public List<Map<String, String>> getAttributes() {
        return attributes;
    }

    @JsonProperty("Attributes")
    public void setAttributes(List<Map<String, String>> attributes) {
        this.attributes = attributes;
    }
}

Here are 3 examples of what the json response might look like,

{
    "CustomId": "9610",
    "Attributes": {}
}


{
    "CustomId": "9610",
    "Attributes": {
            "Test": 0
        }
}

{
    "CustomId": "9610",
    "Attributes": {
            "Test1": 3,
            "Test2": 4
        }
}

This is line I am using to parse the json and where my error occurs.

String result = (... A returned Json string);
strategy = objectMapper.readValue(result, new TypeReference<MyCustomObject>() {});

Lastly, the error I am receiving.

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token....
....[truncated 270 chars]; line: 1, column: 768] 
 (through reference chain: com.model.jackson.MyCustomObject["Attributes"])

Are there any annotations I am missing? Are there any other examples out there of parsing into a list<map...>> ?

CodePudding user response:

In JSON object you have Attributes is an object (Map) but in your MyCustomObject class you trying to convert it to an array (List). So because of that you getting error.

Please change private List<Map<String, String>> attributes; to private Map<String, String> attributes; Remove list then check it will convert data successfully.

@JsonInclude(JsonInclude.Include.ALWAYS)
@JsonPropertyOrder({"CustomId", "Attributes"})

@Generated("jsonschema2pojo")
public class MyCustomObject {

    public MyCustomObject() {}

    @JsonProperty("CustomId")
    private String customId;

    @JsonProperty("Attributes")
    private Map<String, String> attributes;

    @JsonProperty("CustomId")
    public String getCustomId() {
        return customId;
    }

    @JsonProperty("CustomId")
    public void setCustomId(String customId) {
        this.customId = customId;
    }

    @JsonSerialize(keyUsing = MapSerializer.class)
    @JsonProperty("Attributes")
    public Map<String, String> getAttributes() {
        return attributes;
    }

    @JsonProperty("Attributes")
    public void setAttributes(Map<String, String> attributes) {
        this.attributes = attributes;
    }
}
  • Related