json_normalize和多个嵌套字典(SOQL)

时间:2017-09-07 18:21:22

标签: python json pandas

我有一个从SalesForce(SOQL)查询返回的JSON文件,如下例所示,我想使用“json_normalize”来转换pandas dataFrame。

  

以open('SOQL_RESULT.json')作为data_file:     data = json.load(data_file)

     

df = json_normalize(data)

SOQL_RESULT.json 文件:

[
    {
        "attributes": {
            "type": "Opp_Product_Item__c",
            "url": "/services/data/v38.0/sobjects/Opp_Product_Item__c/a0L40000007M7ViEAK"
        },
        "Id": "a0L40000007M7ViEAK",
        "Name": "OPI-273731",
        "Opportunity__c": "0064000000LlgMZAAZ",
        "Opportunity_Solution__c": "a0M400000038XGeEAM",
        "Product__c": "01t400000013pTyAAI",
        "Product_Sales_Team__r": {
            "totalSize": 3,
            "done": true,
            "records": [
                {
                    "attributes": {
                        "type": "Product_Sales_Team__c",
                        "url": "/services/data/v38.0/sobjects/Product_Sales_Team__c/a0X40000001xZfLEAU"
                    },
                    "Id": "a0X40000001xZfLEAU",
                    "Name": "PIT-120844",
                    "Team_Role__c": "Specialist - CPS",
                    "Access__c": "Read/Write",
                    "Product__c": "a0L40000007M7ViEAK"
                },
                {
                    "attributes": {
                        "type": "Product_Sales_Team__c",
                        "url": "/services/data/v38.0/sobjects/Product_Sales_Team__c/a0X40000001xZfQEAU"
                    },
                    "Id": "a0X40000001xZfQEAU",
                    "Name": "PIT-120845",
                    "Team_Role__c": "CSE",
                    "Access__c": "Read/Write",
                    "Product__c": "a0L40000007M7ViEAK"
                },
                {
                    "attributes": {
                        "type": "Product_Sales_Team__c",
                        "url": "/services/data/v38.0/sobjects/Product_Sales_Team__c/a0X40000001xdkbEAA"
                    },
                    "Id": "a0X40000001xdkbEAA",
                    "Name": "PIT-121462",
                    "Team_Role__c": "CSE - Collaboration",
                    "Access__c": "Read",
                    "Product__c": "a0L40000007M7ViEAK"
                }
            ]
        }
    },
    {
        "attributes": {
            "type": "Opp_Product_Item__c",
            "url": "/services/data/v38.0/sobjects/Opp_Product_Item__c/a0L40000009fnUPEAY"
        },
        "Id": "a0L40000009fnUPEAY",
        "Name": "OPI-372996",
        "Opportunity__c": "0064000000QlQmsAAF",
        "Opportunity_Solution__c": "a0M40000004IyUqEAK",
        "Product__c": "01t400000013pTdAAI",
        "Product_Sales_Team__r": {
            "totalSize": 1,
            "done": true,
            "records": [
                {
                    "attributes": {
                        "type": "Product_Sales_Team__c",
                        "url": "/services/data/v38.0/sobjects/Product_Sales_Team__c/a0X40000004N1LlEAK"
                    },
                    "Id": "a0X40000004N1LlEAK",
                    "Name": "PIT-150866",
                    "Team_Role__c": "CSE",
                    "Access__c": "Read/Write",
                    "Product__c": "a0L40000009fnUPEAY"
                }
            ]
        }
    }
]

但不是在 Product_sales_team __。r.records列中有一行嵌套数据,而不是这样:

Id    Name    Opportunity_Solution__c ... Product_Sales_Team__r.records
a...  OP...   a0M...                      [{'attributes': {'type': 'Product_Sales_Team__c', …
a...  OP...   a0M...                      [{'attributes': {'type': 'Product_Sales_Team__c', …

我想在Product_Sales_Team__r下添加3个新列(Team_Role__c,Acces__c和Product__c)。记录在最终的dataFrame中。

我尝试过:

df = json_normalize(data, 'Product_Sales_Team__r[records]', ['Id'], ['Name'], ['Opportunity__c'], 
       record_prefix='Product_Sales_Team__r_records_Name')

但它没有用,基于Google的例子和官方的“json_normalize”文档,我不明白为什么?

以下是所需数​​据框架的外观:

Id    Name    Opportunit...   Team_Role__c    Acces__c    Product__c
a1...  OP...   a0M...           A...           B...        P1...         
a1...  OP...   a0M...           A...           B...        P2...  
a1...  OP...   a0M...           A...           B...        P3...  
a2...  OP...   a0M...           A...           B...        P1...  
a2...  OP...   a0M...           A...           B...        P2...    
a2...  OP...   a0M...           A...           B...        P3...  
...

任何帮助都会受到赞赏!

0 个答案:

没有答案