Parsing any json to collections with maps

Viewed 52

I have Json like below:

{
   "reservations":[
      {
         "id":"1318504",
         "deposit":720,
         "rooms":[
            {
               "name":"room1",
               "id":"28902"
            },
            {
               "name":"room2",
               "id":"28906"
            }
         ],
         "customers":[
            {
               "id":3,
               "country":"UK",
               "firstName":"John"
            }
         ]
      },
      {
         "id":"1318501",
         "deposit":68,
         "rooms":[
            {
               "name":"room3",
               "id":"28860"
            },
            {
               "name":"room4",
               "id":"28886"
            }
         ]
      }
   ]
}

I need to parse this Json to List<Map<String, String>> like below:

Map 1:

"reservations.id":"1318504",
"reservations.deposit":"720",
"reservations.rooms.name":"room1",
"reservations.rooms.id":"28902"
"reservations.customers.id":"3",
"reservations.customers.country": "UK"
"reservations.customers.firstName":"John"

Map 2:

"reservations.id":"1318504"
"reservations.deposit":"720"
"reservations.rooms.name":"room2"
"reservations.rooms.id":"28906" 
"reservations.customers.id":"3"
"reservations.customers.country": "UK",
"reservations.customers.firstName":"John"

    

Map 3:

"reservations.id":"1318501"
"reservations.deposit":"68"
"reservations.room.name":"room3"
"reservations.room.id":"28860"
"reservations.customers.id":""
"reservations.customers.country": ""
"reservations.customers.firstName":""

Map 4:

"reservations.id":"1318501"
"reservations.deposit":"68"
"reservations.room.name":"room4"
"reservations.room.id":"28886"
"reservations.customers.id":""
"reservations.customers.country": ""
"reservations.customers.firstName":"" 

So in words I need to take these two reservations. Then i collect simply attributes and join them with nested attributes (in rooms and customers). Then I join nested attributes.

The main problem is that, this Json is only example. Nested level, number of attributes, their name and values and so on can be any. Further more if attribute exsists in one reservation but in other not - then i put empty value (for example customers in my case).

I try do this:

static List<Map<String, String>> maps = new LinkedList<>();
public static void main(String[] args) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode node = mapper.readTree(new File("pathToAnyJson.json"));
    process(node, "");
    maps.forEach(map -> System.out.println(map));

}

Where process function is:

static void process(JsonNode node, String parentName){
    Map<String,String> values = new LinkedHashMap<>();
    if(node.isObject()){
        ObjectNode objectNode = (ObjectNode) node;
        objectNode.fields().forEachRemaining(entry -> {
            String name = entry.getKey();
            String nodeName = parentName + "." + name;
            JsonNode n = entry.getValue();
            if(!n.isArray() && !n.isObject()){
                values.put(nodeName, n.asText());
            }
            process(n, nodeName);
        } );
        maps.add(values);
    } else if(node.isArray()){
        ArrayNode arrayNode = (ArrayNode) node;
        for(JsonNode jsonNode : arrayNode){
            process(jsonNode,parentName);
        }
    }
}

But with this code I got 6 Maps - 2 for simple attributes from first and second reservations and four Maps from simple attributes from nested rooms. What can I do with this? I'm using jackson for this but if there is better library give me info.

0 Answers
Related