如何使用Python从另一个JSON对象创建新的JSON对象?

如何使用Python从另一个JSON对象创建新的JSON对象?,python,json,cisco,Python,Json,Cisco,我需要创建一个JSON对象,它的信息比原始对象少: 从api服务获取原始JSON对象后,它看起来如下所示: { "queryResponse": { "@type": "AccessPointDetails", "@rootUrl": "https://xxx/webacs/api/v1/data", "@requestUrl": "https://xxx/webacs/api/v1/data/AccessPointDetails?.full=true&.fi

我需要创建一个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
        }
      }
    ]
  }
}
"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
     }
]
我需要创建一个新的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
        }
      }
    ]
  }
}
"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中实现这一点

问候

编辑:一篇重复的文章?如果是这样的话,请不要标记它,你能链接到答案吗,因为我花了几个小时寻找答案?(不适用于袁吉,谢谢你指出单独的资源)

这很简单

  • 将JSON转换为dict
  • 创建另一个dict
  • 从第一个dict中选择您需要的所有内容并将其放到第二个dict中
  • 将第二个dict转换为json

  • 完成

    你的问题只是和的组合

    通过使用这两个问题的解决方案,您可以实现以下目标:

    假设
    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']]}
    
    然后使用以下方法转换
    new\u dict


    与任何“如何将文件转换为文件”一样,将第一个文件加载到合理的对象结构中,然后按您认为合适的方式将其序列化为新格式。不要试图直接将文件结构转换为文本。与任何Python一样,“我有json对象”问题:Python中没有“json对象”这样的东西-json是一种文本格式,它转换为本机数据类型(dicts、list等)。谢谢,我只使用for语句,每个条目都有一行,但您的解决方案更高效。