I have a nested json data like this
{
    "data": {
        "COUNTRIES": [
            {
                "COUNTRY": "INDIA",
                "CITIES": [
                    {
                        "CITY": "BANGALORE",
                        "STORE": [
                            {
                                "STORE_NAME": "amazon",
                                "DEVICE_DETAILS": [
                                    {
                                        "POI": "POI1",
                                        "APPLIANCES": [
                                            {
                                                "APPLIANCE_ID": "1"
                                            },
                                            {
                                                "APPLIANCE_ID": "2"
                                            },
                                            {
                                                "APPLIANCE_ID": "3"
                                            },
                                            {
                                                "APPLIANCE_ID": "4"
                                            },
                                            {
                                                "APPLIANCE_ID": "5"
                                            },
                                            {
                                                "APPLIANCE_ID": "6"
                                            },
                                            {
                                                "APPLIANCE_ID": "7"
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "CITY": "DELHI",
                        "STORE": [
                            {
                                "STORE_NAME": "amazon",
                                "DEVICE_DETAILS": [
                                    {
                                        "POI": "POI1",
                                        "APPLIANCES": [
                                            {
                                                "APPLIANCE_ID": "1"
                                            },
                                            {
                                                "APPLIANCE_ID": "2"
                                            },
                                            {
                                                "APPLIANCE_ID": "3"
                                            },
                                            {
                                                "APPLIANCE_ID": "4"
                                            },
                                            {
                                                "APPLIANCE_ID": "5"
                                            },
                                            {
                                                "APPLIANCE_ID": "6"
                                            },
                                            {
                                                "APPLIANCE_ID": "7"
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    }
                ]
            },
            {
                "COUNTRY": "UNITED KINGDOM",
                "CITIES": [
                    {
                        "CITY": "BIRMINGHAM",
                        "STORE": []
                            }
                        ]
                    }
                ]
            }
        ]
    }
}
which i have to modify and count of each array at each level resulting in something like this
{
    "data": {
        "COUNTRIES": [
            {
                "COUNTRY": "INDIA",
                "CITIES": [
                    {
                        "CITY": "BANGALORE",
                        "STORE": [
                            {
                                "STORE_NAME": "AMAZON",
                                "DEVICE_DETAILS": [
                                    {
                                        "POI": "POI1",
                                        "APPLIANCES": [
                                            {
                                                "APPLIANCE_ID": "1"
                                            },
                                            {
                                                "APPLIANCE_ID": "2"
                                            },
                                            {
                                                "APPLIANCE_ID": "3"
                                            },
                                            {
                                                "APPLIANCE_ID": "4"
                                            },
                                            {
                                                "APPLIANCE_ID": "5"
                                            },
                                            {
                                                "APPLIANCE_ID": "6"
                                            },
                                            {
                                                "APPLIANCE_ID": "7"
                                            }
                                        ]
                                    }
                                ],
                                "DEVICE_CNT": 7,
                                "APPLIANCES_CNT": 35
                            }
                        ],
                        "STORE_CNT": 1,
                        "DEVICE_CNT": 7,
                        "APPLIANCES_CNT": 35
                    },
                    {
                        "CITY": "DELHI",
                        "STORE": [],
                        "STORE_CNT": 0
                    }
                ],
                "CITY_CNT": 2,
                "STORE_CNT": 1,
                "DEVICE_CNT": 7,
                "APPLIANCES_CNT": 35
            },
            {
                "COUNTRY": "UNITED KINGDOM",
                "CITIES": [
                    {
                        "CITY": "BIRMINGHAM",
                        "STORE": [
                            {
                                "STORE_NAME": "M&S",
                                "LATTITUDE": 52.486243,
                                "LONGITUDE": -1.890401,
                                "DEVICE_DETAILS": [
                                    {
                                        "POI": "POI1",
                                        "APPLIANCES": [
                                            {
                                                "APPLIANCE_ID": "1"
                                            },
                                            {
                                                "APPLIANCE_ID": "2"
                                            },
                                            {
                                                "APPLIANCE_ID": "3"
                                            },
                                            {
                                                "APPLIANCE_ID": "4"
                                            }
                                        ],
                                        "APPLIANCES_CNT": 4
                                    }
                                ],
                                "DEVICE_CNT": 1,
                                "APPLIANCES_CNT": 4
                            }
                        ],
                        "STORE_CNT": 1,
                        "DEVICE_CNT": 1,
                        "APPLIANCES_CNT": 4
                    }
                ],
                "CITY_CNT": 1,
                "STORE_CNT": 1,
                "DEVICE_CNT": 1,
                "APPLIANCES_CNT": 4
            }
        ],
        "COUNTRY_CNT": 2,
        "CITY_CNT": 3,
        "STORE_CNT": 2,
        "DEVICE_CNT": 8,
        "APPLIANCES_CNT": 39
    }
}
I'm unable to get the total count at the upper level,for ex. at CITIES,the total count of appliances must be the total count of all appliances in BANGLORE.I'm only able to get the count at a particular level but not above it.
This is what i have done till now
let levels = {
  0: ["COUNTRY_CNT", "CITY_COUNT", "STORE_COUNT", "DEVICE_COUNT", "APPLIANCES_COUNT"],
  1: ["CITY_COUNT", "STORE_COUNT", "DEVICE_COUNT", "APPLIANCES_COUNT"],
  2: ["STORE_COUNT", "DEVICE_COUNT", "APPLIANCES_COUNT"],
  3: ["DEVICE_COUNT", "APPLIANCES_COUNT"],
  4: ["APPLIANCES_COUNT"]
};
let counts = {COUNTRIES:0,CITIES:0,STORE:0,DEVICE_DETAILS:0,APPLIANCES:0};
let count_mapping = {
  CITY_COUNT:"CITIES",
  STORE_COUNT:"STORE",
  DEVICE_COUNT: "DEVICE_DETAILS",
  APPLIANCES_COUNT: "APPLIANCES",
  COUNTRY_CNT:"COUNTRIES"
};
let total_count= {COUNTRIES:[],CITIES:[],STORE:[],DEVICE_DETAILS:[],APPLIANCES:[]};
  async function parse(dictionary,level) {
    console.log(dictionary);
    for (item in dictionary) {
      counts = {COUNTRIES:0,CITIES:0,STORE:0,DEVICE_DETAILS:0,APPLIANCES:0};
      for (subItem in dictionary[item]) {
        let b = item;
        if (dictionary[b]) {
          let sub = subItem;
          let main = dictionary[b][subItem];
          if (Array.isArray(dictionary[b][subItem])) {
           await parse(dictionary[b][subItem],level+1);
            if ( dictionary[b] && Array.isArray(main)){
              counts[sub]=dictionary[b][sub].length;
            }
            await add(b,dictionary,level);
          }
        }
      }
    }
  }
  function add(b,dictionary,level) {
    for(params in levels[level]){
      if(dictionary[b])
        dictionary[b][levels[level][params]] = counts[count_mapping[levels[level][params]]]
    }
  }
How do i get my desired output?
 
    