Map JSON To List<Map<<String, Object>>

Pranay Kumar picture Pranay Kumar · Jun 22, 2017 · Viewed 26.3k times · Source

I have a JSON of the format

[{
    "id" : "a01",
    "name" : "random1",
    "val" : "random2"

},
{
    "id" : "a03",
    "name" : "random3",
    "val" : "random4"
}]

I need to map it to a List holding various Map objects. How do I achieve it?

Even if I am able to convert this JSON to a List of String of the form

{
    "id" : "a01",
    "name" : "random1",
    "val" : "random2"

}

then I have a method to convert each individual String to a Map.

Answer

Manos Nikolaidis picture Manos Nikolaidis · Jun 22, 2017

You will need to pass a TypeReference to readValue with the desired result type:

ObjectMapper mapper = new ObjectMapper();
List<Map<String, Object>> data = mapper.readValue(json, new TypeReference<List<Map<String, Object>>>(){});