2017-11-27 6 views
0

Ich brauche ein JSON-Objekt mit weniger Informationen zu machen, als das Original:Wie kann ich mit Python ein neues JSON-Objekt erstellen?

Das ursprüngliche JSON-Objekt, nachdem ich es von dem api-Dienst wie folgt aussieht erhalten:

{ 
    "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 
     } 
     } 
    ] 
    } 
} 

ich eine neue JSON erstellen muß Objekt mit nur wenigen Informationen aus dem oben, wie folgt aus:

"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 
    } 
] 

Wenn Sie schauen, ist das Unternehmen Liste in einem Wörterbuch in den Originaldaten.

Ich habe versucht, Informationen darüber zu finden, aber ich fand nur Informationen, wie Sie löschen, oder wie jedes Mal 1 Daten zu extrahieren.

Wie kann ich dies in Python tun?

Grüße.

Edit: Ein Duplikat Post? Wenn das so ist, anstatt das zu markieren, können Sie bitte auf die Antwort verlinken, weil ich mehrere Stunden auf der Suche nach einer Antwort verbrachte? (Nicht für Yuan Ji, danke für das Zeigen dieser separaten Ressourcen)

+2

Wie bei jeder „wie kann ich umwandeln Datei in Datei“ - laden Sie die erste Datei in eine sinnvolle Objektstruktur, und dann, dass Serialisierung in das neue Format, wie Sie es für richtig halten. Versuchen Sie nicht, Dateistrukturen direkt als Text zu konvertieren. – Bilkokuya

+1

Wie bei jeder Python-Frage "Ich habe ein Json-Objekt": In Python gibt es kein "json-Objekt" - json ist ein _text-Format_, das in nativen Datentypen (dicts, Listen usw.) konvertiert wird. –

Antwort

1

Ihre Frage ist nur eine Kombination von python getting a list of value from list of dict und Filter dict to contain only certain keys?

von Lösungen beider Fragen verwenden, können Sie es erreichen:

Sagen wir s die dict von Ihrem JSON-Daten umgewandelt wird.

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']]} 

konvertieren Dann new_dict von json.dumps mit:

your_json_object = json.dumps(new_dict) 
+0

Danke, ich benutze nur eine for-Anweisung mit einer Zeile für jeden Eintrag, aber Ihre Lösung ist effizienter. – Maxtrix

1

Dies ist einfach. Konvertieren Sie die zweite dict in json

  1. Convert JSON
  2. Erstellen Sie eine andere dict
  3. Wählen Sie alles, was Sie von der ersten dict müssen dict und es in die zweite
  4. setzen

Geschehen

Verwandte Themen