krishnakumarp krishnakumarp - 3 months ago 26
JSON Question

Jackson json bind String and Array to List<String> inside a Map

I would like to map json string and array to a Java List. The Java class is as below.

public class MyDTO {

private String identifier;

private Map<String, List<String>> parameters;

public String getIdentifier() {
return identifier;
public void setIdentifier(String identifier) {
this.identifier = identifier;
public Map<String, List<String>> getParameters() {
return parameters;
public void setParameters(Map<String, List<String>> parameters) {
this.parameters = parameters;


I would like both the following json samples to map correctly to the above Java class.


{ "identifier": "myIdentifier", "parameters":{
"param1": "value" } }


{ "identifier": "myIdentifier", "parameters":{
"param1": ["value1", "value2"] } }

I've tried using ACCEPT_SINGLE_VALUE_AS_ARRAY for the Map property, but I think it is not getting applied to the inner List

Any solutions?



@JsonFormat is not the appropriate annotation to configure deserialization of such kind. Annotation based POJO configuration will not yield the correct result. Instead, accept the raw JSON string and later deserialize with objectmapper with the needed configuration

String json1 = "{ \"identifier\": \"myIdentifier\", \"parameters\":{ \"param1\": \"value\" } }\n";
String json2 = "{ \"identifier\": \"myIdentifier\", \"parameters\":{ \"param1\": [\"value1\", \"value2\"] } }";
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
try {
  MyDTO obj1 = mapper.readValue(json1, MyDTO.class);
  MyDTO obj2 = mapper.readValue(json2, MyDTO.class);
}catch (Exception e){
  // handle