I need to make a JSON object with less information than the original:
The original JSON object after I get it from the api service looks like this:
{
"queryResponse": {
"@type": "AccessPointDetails",
"@rootUrl": "https://xxx/webacs/api/v1/data",
"@requestUrl": "https://xxx/webacs/api/v1/data/AccessPointDetails?.full=true&.firstResult=250&.maxResults=2",
"@responseType": "listEntityInstances",
"@count": "347",
"@first": "250",
"@last": "251",
"entity": [
{
"@url": "https://xxx/webacs/api/v1/data/AccessPointDetails/1897332",
"@type": "AccessPointDetails",
"@dtoType": "accessPointDetailsDTO",
"accessPointDetailsDTO": {
"@id": "1897332",
"@displayName": "1897332",
"adminStatus": "ENABLE",
"apType": "AP1140",
"clientCount": 1,
"clientCount_2_4GHz": 1,
"clientCount_5GHz": 0,
"ethernetMac": "xxx",
"ipAddress": "xxx",
"locationHeirarchy": "Root Area",
"macAddress": "xxx",
"mapLocation": "xxx",
"model": "AIR-LAP1141N-A-K9",
"name": "xxx",
"serialNumber": "xxx",
"softwareVersion": "8.0.140.0",
"status": "CLEARED",
"type": "UnifiedAp",
"unifiedApInfo": {
"instanceId": 0,
"instanceVersion": 0,
"apCertType": 1,
"apGroupName": "xxx",
"apMode": 2,
"apStaticEnabled": 0,
"bootVersion": "12.4.23.6",
"capwapJoinTakenTime": 1500,
"capwapUpTime": 52444681,
"controllerIpAddress": "xxx",
"controllerName": "wlc_5508",
"contryCode": "US",
"encryptionEnabled": false,
"flexConnectMode": false,
"iosVersion": "15.3(3)JA10$",
"linkLatencyEnabled": false,
"poeStatus": 5,
"portNumber": 13,
"powerInjectorState": 1,
"preStandardState": 0,
"primaryMwar": "xxx",
"rogueDetectionEnabled": true,
"sshEnabled": false,
"statisticsTimer": 180,
"telnetEnabled": false,
"vlanEnabled": true,
"vlanNativeId": 16,
"WIPSEnabled": 0,
"wlanVlanMappings": {
"wlanVlanMapping": {
"ssid": "xxx",
"vlanId": 220,
"wlanId": 3
}
}
},
"upTime": 2666643681
}
},
{
"@url": "https://xxx/webacs/api/v1/data/AccessPointDetails/1897334",
"@type": "AccessPointDetails",
"@dtoType": "accessPointDetailsDTO",
"accessPointDetailsDTO": {
"@id": "1897334",
"@displayName": "1897334",
"adminStatus": "ENABLE",
"apType": "AP3500E",
"clientCount": 8,
"clientCount_2_4GHz": 8,
"clientCount_5GHz": 0,
"ethernetMac": "xxx",
"ipAddress": "xxx",
"locationHeirarchy": "Root Area",
"macAddress": "xxx",
"mapLocation": "xxx",
"model": "AIR-CAP3501E-A-K9",
"name": "xxx",
"serialNumber": "xxx",
"softwareVersion": "8.1.131.0",
"status": "CLEARED",
"type": "UnifiedAp",
"unifiedApInfo": {
"instanceId": 0,
"instanceVersion": 0,
"apCertType": 1,
"apGroupName": "xxx",
"apMode": 2,
"apStaticEnabled": 0,
"bootVersion": "15.3.2.4",
"capwapJoinTakenTime": 1500,
"capwapUpTime": 52445240,
"controllerIpAddress": "xxx",
"controllerName": "wlc_5520",
"contryCode": "US",
"encryptionEnabled": false,
"flexConnectMode": false,
"iosVersion": "15.3(3)JBB6$",
"linkLatencyEnabled": false,
"poeStatus": 5,
"portNumber": 8,
"powerInjectorState": 1,
"preStandardState": 0,
"primaryMwar": "Cisco_10:2d:ae",
"rogueDetectionEnabled": true,
"sshEnabled": false,
"statisticsTimer": 180,
"telnetEnabled": false,
"vlanEnabled": true,
"vlanNativeId": 21,
"WIPSEnabled": 0,
"wlanVlanMappings": {
"wlanVlanMapping": {
"ssid": "xxx",
"vlanId": 220,
"wlanId": 1
}
}
},
"upTime": 2399985140
}
}
]
}
}
I need to create a new JSON object with only a few information from the above, like this:
"entity": [
{
"@id": 12345,
"name": asdf,
"clienCount": 5,
"clientCount_2_4GHz": 3,
"clientCount_5GHz": 2
},
{
"@id": 12345,
"name": asdf,
"clienCount": 5,
"clientCount_2_4GHz": 3,
"clientCount_5GHz": 2
}
]
If you look, the entity list is inside a dictionary in the original data.
I tried to find information about this but I only found information how to delete, or how to extract 1 data each time.
How I can do this in Python?
Regards.
Edit: A duplicate post?, if that so, instead of flagging that, can you please link to the answer because I spent several hours looking for an answer? (Not for Yuan Ji, thanks for pointing that separate resources)
Your question is just a combination of python getting a list of value from list of dict and Filter dict to contain only certain keys?
By using solutions of both questions, you could achieve it:
Let's say s
is the dict
converted from your json data.
keys = ['@id', 'name', 'clientCount', 'clientCount_2_4GHz', 'clientCount_5GHz']
new_dict = {'entity': [{key: d['accessPointDetailsDTO'][key] for key in keys} for d in s['queryResponse']['entity']]}
Then convert new_dict
by using json.dumps
:
your_json_object = json.dumps(new_dict)