2016-10-20 7 views
2

ich eine verschachtelte JSON haben, wie unten gezeigt und wollen in mehrere Datenrahmen in Python analysieren .. bitteverschachtelte JSON in mehrere Datenrahmen Parsen Pandas Python mit

{ 
"tableName": "cases", 
"url": "EndpointVoid", 
"tableDataList": [{ 
    "_id": "100017252700", 
    "title": "Test", 
    "type": "TECH", 
    "created": "2016-09-06T19:00:17.071Z", 
    "createdBy": "193164275", 
    "lastModified": "2016-10-04T21:50:49.539Z", 
    "lastModifiedBy": "1074113719", 
    "notes": [{ 
     "id": "30", 
     "title": "Multiple devices", 
     "type": "INCCL", 
     "origin": "D", 
     "componentCode": "PD17A", 
     "issueCode": "IP321", 
     "affectedProduct": "134322", 
     "summary": "testing the json", 

     "caller": { 
      "email": "[email protected]", 
      "phone": "651-744-4522" 
     } 
    }, { 
     "id": "50", 
     "title": "EDU: Multiple Devices - Lightning-to-USB Cable", 
     "type": "INCCL", 
     "origin": "D", 
     "componentCode": "PD17A", 
     "issueCode": "IP321", 
     "affectedProduct": "134322", 
     "summary": "parsing json 2", 
     "caller": { 
      "email": "[email protected]", 
      "phone": "123-345-1111" 
     } 
    }], 
    "syncCount": 2316, 
    "repair": [{ 
      "id": "D208491610", 
      "created": "2016-09-06T19:02:48.000Z", 
      "createdBy": "193164275", 
      "lastModified": "2016-09-21T12:49:47.000Z" 
     }, { 
      "id": "D208491610" 
     }, { 
      "id": "D208491628", 
      "created": "2016-09-06T19:03:37.000Z", 
      "createdBy": "193164275", 
      "lastModified": "2016-09-21T12:49:47.000Z" 
     } 

    ], 
    "enterpriseStatus": "8" 
}], 
"dateTime": 1475617849, 
"primaryKeys": ["$._id"], 
"primaryKeyVals": ["100017252700"], 
"operation": "UPDATE" 

}

Ich möchte dabei helfen analysieren und erstellen 3 Tabellen/Datenrahmen/csv, wie unten dargestellt .. bitte helfen ..

Output table in this format

+0

Ich denke, Ihre json nicht gültig ist - überprüfen Sie bitte [http://jsonlint.com /](http://jsonlint.com/) – jezrael

+0

Danke jezrael für lettin g mir wissen .. Es ist Kopie einfügen Fehler .. Ich habe gerade die JSON-Datei behoben .. – Raj

Antwort

0

ich glaube nicht, dass dies am besten Weg ist, aber ich wollte Ihnen zeigen, possib iilität.

import pandas as pd 
from pandas.io.json import json_normalize 
import json 

with open('your_sample.json') as f:  
    dt = json.load(f) 

Table1

df1 = json_normalize(dt, 'tableDataList', 'dateTime')[['_id', 'title', 'type', 'created', 'createdBy', 'lastModified', 'lastModifiedBy', 'dateTime']] 
print df1 


      _id title type     created createdBy \ 
0 100017252700 Test TECH 2016-09-06T19:00:17.071Z 193164275 

       lastModified lastModifiedBy dateTime 
0 2016-10-04T21:50:49.539Z  1074113719 1475617849 

Tabelle 2

df2 = json_normalize(dt['tableDataList'], 'notes', '_id') 
df2['phone'] = df2['caller'].map(lambda x: x['phone']) 
df2['email'] = df2['caller'].map(lambda x: x['email']) 
df2 = df2[['_id', 'id', 'title', 'email', 'phone']] 
print df2 


      _id id           title \ 
0 100017252700 30        Multiple devices 
1 100017252700 50 EDU: Multiple Devices - Lightning-to-USB Cable 

        email   phone 
0 [email protected] 651-744-4522 
1  [email protected] 123-345-1111 

Tabelle 3

df3 = json_normalize(dt['tableDataList'], 'repair', '_id').dropna() 
print df3 


        created createdBy   id    lastModified \ 
0 2016-09-06T19:02:48.000Z 193164275 D208491610 2016-09-21T12:49:47.000Z 
2 2016-09-06T19:03:37.000Z 193164275 D208491628 2016-09-21T12:49:47.000Z 

      _id 
0 100017252700 
2 100017252700 
+0

dieser Code funktioniert .. im Grunde ich exportiere Daten in JSON von Mongodb und wenn ich mehrere Fall Datensätze den Code nicht funktioniert und irgendwann Paar von Spalten werden nicht in JSON gefüllt und wieder json Index nicht verfügbar Problem konfrontiert ... – Raj

Verwandte Themen