I'm trying to transform keys of the items "datacenter_clusters" to a key/value pair in itself. This will also to loop through the following list of dict :
"list_of_dict": [
{
"datacenter": "DC_A",
"datacenter_clusters": {
"CLUSTER_ALPHA": {
"datacenter": "DC_A",
"cluster_info_1": "xxxxxx",
"cluster_info_2": true,
"hosts": [
{
"folder": "/path/to/host-1",
"name": "hostname-1"
},
{
"folder": "/path/to/host-2",
"name": "hostname-2"
}
},
"CLUSTER_BETA": {
"datacenter": "DC_A",
"cluster_info_1": "yyyyyyy",
"cluster_info_2": false,
}
}
"vcenter": "vcenter-1"
},
{
"datacenter": "DC_B",
"datacenter_clusters": {
"CLUSTER_OMEGA": {
"datacenter": "DC_B",
"cluster_info_1": "aaaaaaaa",
"cluster_info_2": true,
},
"CLUSTER_ZETA": {
"datacenter": "DC_A",
"cluster_info_1": "bbbbbbbbbb",
"cluster_info_2": false,
}
}
"vcenter": "vcenter-2"
}
]
and add the new key/value pair "cluster_name":"<KEY_OF_PARENT_ELEMENT>" thus transforming the dict "datacenter_clusters" to a list. The expected result should look like :
"list_of_dict": [
{
"datacenter": "DC_A",
"datacenter_clusters": [
{
"cluster_name": "CLUSTER_ALPHA",
"datacenter": "DC_A",
"cluster_info_1": "xxxxxx",
"cluster_info_2": true,
"hosts": [
{
"folder": "/path/to/host-1",
"name": "hostname-1"
},
{
"folder": "/path/to/host-2",
"name": "hostname-2"
}
]
},
{
"cluster_name": "CLUSTER_ALPHA",
"datacenter": "DC_A",
"cluster_info_1": "yyyyyyy",
"cluster_info_2": false,
}
]
"vcenter": "vcenter-1"
},
{
"datacenter": "DC_B",
"datacenter_clusters": [
{
"cluster_name": "CLUSTER_OMEGA"
"datacenter": "DC_B",
"cluster_info_1": "aaaaaaaa",
"cluster_info_2": true,
},
{
"cluster_name": "CLUSTER_ZETA"
"datacenter": "DC_A",
"cluster_info_1": "bbbbbbbbbb",
"cluster_info_2": false,
}
]
"vcenter": "vcenter-2"
}
]
I tried to combine what I found in this link with "with_subelements" but I couldn't loop through a list of dicts, I could only loop through a simple dict or a simple list or a list of lists. Any help would be much appreciated