如何使用 Python 从另一个对象创建一个新的 JSON 对象?
How can I create a new JSON object form another using Python?
我需要制作一个 JSON 对象,其信息比原始对象少:
我从 api 服务中获取的原始 JSON 对象如下所示:
{
"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
}
}
]
}
}
我需要用上面的一些信息创建一个新的 JSON 对象,如下所示:
"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
}
]
如果你看,实体列表在原始数据中的字典中。
我试图查找有关此的信息,但我只找到有关如何删除或每次提取 1 条数据的信息。
如何在 Python 中执行此操作?
此致。
编辑:重复 post?如果是这样,请您 link 回答一下,而不是标记它,因为我花了几个小时寻找答案? (不是袁霁,谢谢指点那个单独的资源)
这很简单。
- 将JSON转换为字典
- 创建另一个字典
- Select 你需要的一切从第一个字典到第二个字典
- 将第二个dict转换为json
完成
你的问题只是python getting a list of value from list of dict and Filter dict to contain only certain keys?
的组合
通过使用两个问题的解决方案,您可以实现它:
假设 s
是从您的 json 数据转换而来的 dict
。
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']]}
然后使用json.dumps
转换new_dict
:
your_json_object = json.dumps(new_dict)
我需要制作一个 JSON 对象,其信息比原始对象少:
我从 api 服务中获取的原始 JSON 对象如下所示:
{
"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
}
}
]
}
}
我需要用上面的一些信息创建一个新的 JSON 对象,如下所示:
"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
}
]
如果你看,实体列表在原始数据中的字典中。
我试图查找有关此的信息,但我只找到有关如何删除或每次提取 1 条数据的信息。
如何在 Python 中执行此操作?
此致。
编辑:重复 post?如果是这样,请您 link 回答一下,而不是标记它,因为我花了几个小时寻找答案? (不是袁霁,谢谢指点那个单独的资源)
这很简单。
- 将JSON转换为字典
- 创建另一个字典
- Select 你需要的一切从第一个字典到第二个字典
- 将第二个dict转换为json
完成
你的问题只是python getting a list of value from list of dict and Filter dict to contain only certain keys?
的组合通过使用两个问题的解决方案,您可以实现它:
假设 s
是从您的 json 数据转换而来的 dict
。
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']]}
然后使用json.dumps
转换new_dict
:
your_json_object = json.dumps(new_dict)