Home > Software engineering >  How to remove 'attributes' from SalesForce JSON data by Python?
How to remove 'attributes' from SalesForce JSON data by Python?

Time:02-10

I have developed a Python script to get data from SalesForce and write them to csv file. I like to use DataFrame for this.

Below is data that I retrieve from SalesForce by call API. I used some relationship fields in my SQL script, so I want to delete 'attributes' from JSON data. I tried "drop columns" as below but it only work for parent fields and still have many "attributes" for relationship fields inside.

response = browser.json()
df = pd.DataFrame(response['records']).drop(columns = 'attributes')

Sample DataFrame output:

            Id                                  EmpPeriodID_PP__r  ...  
0  1A1v00000Av  {'**attributes**': {'type': 'Employment_Period__c'...  ...  
1  1A1v00000Aw  {'**attributes**': {'type': 'Employment_Period__c'...  ...  

[2 rows x 14 columns]

JSON example:

{
    'Id': 'a1A1v00000',
    'EmpID_PP__c': 'a0T1v00000NM',
    'EmpID_PP__r': {
        '**attributes**': {
            'type': 'Employment__c',
            'url': '/services/data/v47.0/sobjects/Employment__c/a0T1v00000'
        },
        'EmpID_EP__c': 'a0U1v00000',
        'EmpID_EP__r': {
            '**attributes**': {
                'type': 'Employment__c',
                'url': '/services/data/v47.0/sobjects/Employment__c/a0U1v00000'
            },
            'OrgID__r': {
                '**attributes**': {
                    'type': 'Account',
                    'url': '/services/data/v47.0/sobjects/Account/0011v'
                },
                'OrgId__c': '100'
            },
    },

Do you know any solution to remove all 'attributes' in JSON data then them will be disappeared in DataFrame?

Thank you.

CodePudding user response:

  •  Tags:  
  • Related