i have api call response as below,
x = response.json()
{'selectedColumns': [], 'totalRows': 2, 'pageNumber': 0, 'pageSize': 0, 'searchResults':
{'items': [
{'id': '199', 'tiName': 'host-a', 'cmbMake': 'Cisco Systems', 'cmbModel': 'Catalyst C2960G-48TC-L', 'tiCustomField_CDP Neighbors': "[{'dest_host': ' AAA', 'sysname': ''}, {'dest_host': ' BBB', 'sysname': ''}]"},
{'id': '1504', 'tiName': 'host-b', 'cmbMake': 'Cisco Systems', 'cmbModel': 'Catalyst C2960-48TC-L', 'tiCustomField_CDP Neighbors': "[{'dest_host': ' CCC', 'sysname': ''}, {'dest_host': ' DDD', 'sysname': ''}]"}
]}}
What I have tried:
y = x['searchResults']['items']
keys = {"tiCustomField_CDP Neighbors","id"}
res = {}
l = []
for i in y:
for k, v in i.items():
if k in keys:
sub = {k: v}
res.update(sub)
l.append(res)
out = l
print(out)
based on above code i am getting output as:
[{
"id": "1504",
"tiCustomField_CDP Neighbors": "[{'dest_host': ' CCC', 'sysname': ''}, {'dest_host': ' DDD', 'sysname': ''}]"
},
{
"id": "1504",
"tiCustomField_CDP Neighbors": "[{'dest_host': ' CCC', 'sysname': ''}, {'dest_host': ' DDD', 'sysname': ''}]"
}]
the way i wanted is:
[
{
"id": "199",
"tiCustomField_CDP Neighbors": "[{'dest_host': ' AAA', 'sysname': ''}, {'dest_host': ' BBB', 'sysname': ''}]"
},
{
"id": "1504",
"tiCustomField_CDP Neighbors": "[{'dest_host': ' CCC', 'sysname': ''}, {'dest_host': ' DDD', 'sysname': ''}]"
}
]
Please any one suggest the right way of for loop so that i get above output.
i am running python3. also i am new to python. still trying to learn it.apologies for any mistakes that i did.