user=""" { "totalSize": 3, "done": true, "records": [ { "attributes": { "type": "Opportunity", "url": "0064I00000qtnhjdujdjds98" }, "Id": "774ndklsk90", "Name": "Excel", "Opportunity_Lead__r": { "attributes": { "type": "User", "url": "fjdjdnxfdjf7r038" }, "Name": "Vladimir Ivanov" }, "Opportunity_Lead__c": "747fhdkdk", "CreatedDate": "2019-04-05T07:14:13.000+0000", "Delivery_Start_Date__c": "2019-04-05", "Delivery_End_Date__c": "2019-04-30", "Amount": 3000.0, "StageName": "Won", "CloseDate": "2019-04-05", "Opportunity_Owner__c": "Petr Amir", "Owner": { "attributes": { "type": "User", "url": "fhf87eedjsfhd6400037fh" }, "Cost_Centre_Code__c": "FGHKD" }, "OpportunityLineItems": { "totalSize": 1, "done": true, "records": [ { "attributes": { "type": "OpportunityLineItem", "url": "fhfhdydlsms5749djx" }, "ProductCode": "TYDL0" } ] } }, { "attributes": { "type": "Opportunity", "url": "t7hhdlksfuFJDUD4899" }, "Id": "58FJFKD", "Name": "Shell", "Opportunity_Lead__r": { "attributes": { "type": "User", "url": "jjddjd857dh5758FJFDJ" }, "Name": "Vladimir Sytov" }, "Opportunity_Lead__c": "8r8rjfdkdkdb", "CreatedDate": "2019-06-11T15:11:17.000+0000", "Delivery_Start_Date__c": "2019-11-01", "Delivery_End_Date__c": "2020-03-31", "Amount": 500.0, "StageName": "Won", "CloseDate": "2020-03-27", "Opportunity_Owner__c": "Kiril Alexeev", "Owner": { "attributes": { "type": "User", "url": "fjd7574kdjd00047dhsk" }, "Cost_Centre_Code__c": "HGFL" }, "OpportunityLineItems": { "totalSize": 1, "done": true, "records": [ { "attributes": { "type": "OpportunityLineItem", "url": "jf84hd00k4I00001q91OhQAI" }, "ProductCode": "JFKD" } ] } }, { "attributes": { "type": "Opportunity", "url": "575fjdlks599RFJJDH" }, "Id": "85JFKD0", "Name": "BMW", "Opportunity_Lead__r": { "attributes": { "type": "User", "url": "gfhf7re99RJFDD7DMKS" }, "Name": "Andrey Buglak" }, "Opportunity_Lead__c": "858jfhdgsk", "CreatedDate": "2019-09-19T11:52:47.000+0000", "Delivery_Start_Date__c": "2019-10-07", "Delivery_End_Date__c": "2020-08-31", "Amount": 1200.0, "StageName": "Won", "CloseDate": "2020-01-27", "Opportunity_Owner__c": "Anna Karpenko", "Owner": { "attributes": { "type": "User", "url": "gu7r903jd588fdhk0d" }, "Cost_Centre_Code__c": "JGFDL" }, "OpportunityLineItems": { "totalSize": 1, "done": true, "records": [ { "attributes": { "type": "OpportunityLineItem", "url": "757hfkd0s00k4I00001spr" }, "ProductCode": "P043" } ] } } ] } """
import json user=""" { "totalSize": 3, "done": true, "records": [ { "attributes": { "type": "Opportunity", "url": "/services/data/v46.0/sobjects/Opportunity/0064I00000qtnCHQAY" }, "Id": "0064I00000qtnCHQAY", "Name": "Excel", "Opportunity_Lead__r": { "attributes": { "type": "User", "url": "/services/data/v46.0/sobjects/User/fdjf7r038" }, "Name": "Vladimír Ivanov" }, "Opportunity_Lead__c": "747fhdkdk", "CreatedDate": "2019-04-05T07:14:13.000+0000", "Delivery_Start_Date__c": "2019-04-05", "Delivery_End_Date__c": "2019-04-30", "Amount": 3000.0, "StageName": "Won", "CloseDate": "2019-04-05", "Opportunity_Owner__c": "Petr Amir", "Owner": { "attributes": { "type": "User", "url": "/services/data/v46.0/sobjects/User/fhd6400037fh" }, "Cost_Centre_Code__c": "FGHKD" }, "OpportunityLineItems": { "totalSize": 1, "done": true, "records": [ { "attributes": { "type": "OpportunityLineItem", "url": "/services/data/v46.0/sobjects/OpportunityLineItem/00k4I00001k7uxfQAA" }, "ProductCode": "TYDL0" } ] } }, { "attributes": { "type": "Opportunity", "url": "/services/data/v46.0/sobjects/Opportunity/FJDUD4899" }, "Id": "58FJFKD", "Name": "Shell Data Governance", "Opportunity_Lead__r": { "attributes": { "type": "User", "url": "/services/data/v46.0/sobjects/User/5758FJFDJ" }, "Name": "Vladimir Sytov" }, "Opportunity_Lead__c": "8r8rjfdkdkdb", "CreatedDate": "2019-06-11T15:11:17.000+0000", "Delivery_Start_Date__c": "2019-11-01", "Delivery_End_Date__c": "2020-03-31", "Amount": 500.0, "StageName": "Won", "CloseDate": "2020-03-27", "Opportunity_Owner__c": "Kiril Alexeev", "Owner": { "attributes": { "type": "User", "url": "/services/data/v46.0/sobjects/User/00047dhsk" }, "Cost_Centre_Code__c": "HGFL" }, "OpportunityLineItems": { "totalSize": 1, "done": true, "records": [ { "attributes": { "type": "OpportunityLineItem", "url": "/services/data/v46.0/sobjects/OpportunityLineItem/00k4I00001q91OhQAI" }, "ProductCode": "JFKD" } ] } }, { "attributes": { "type": "Opportunity", "url": "/services/data/v46.0/sobjects/Opportunity/599RFJJDH" }, "Id": "85JFKD0", "Name": "BMW", "Opportunity_Lead__r": { "attributes": { "type": "User", "url": "/services/data/v46.0/sobjects/User/99RJFDD7DMKS" }, "Name": "Andrey Buglak" }, "Opportunity_Lead__c": "858jfhdgsk", "CreatedDate": "2019-09-19T11:52:47.000+0000", "Delivery_Start_Date__c": "2019-10-07", "Delivery_End_Date__c": "2020-08-31", "Amount": 1200.0, "StageName": "Won", "CloseDate": "2020-01-27", "Opportunity_Owner__c": "Anna Karpenko", "Owner": { "attributes": { "type": "User", "url": "/services/data/v46.0/sobjects/User/588fdhk0d" }, "Cost_Centre_Code__c": "JGFDL" }, "OpportunityLineItems": { "totalSize": 1, "done": true, "records": [ { "attributes": { "type": "OpportunityLineItem", "url": "/services/data/v46.0/sobjects/OpportunityLineItem/00k4I00001spr" }, "ProductCode": "P043" } ] } } ] } """ data = json.loads(user) rec = data['records'][0] def f(rec): new = {} for k, v in rec.items(): if isinstance(v, dict): for k1, v1 in v.items(): if isinstance(v1, dict) and k != k1: for k2, v2 in v.items(): if isinstance(v2, dict) and k1 != k2: for k3, v3 in v.items(): new[f'{k}_{k1}_{k2}_{k3}'] = v3 else: new[f'{k}_{k1}_{k2}'] = v2 else: new[f'{k}_{k1}'] = v1 else: new[f'{k}']=v return new for k,v in f(rec).items(): print(f'{k}: {v}')
attributes_type: Opportunity attributes_url: /services/data/v46.0/sobjects/Opportunity/0064I00000qtnCHQAY Id: 0064I00000qtnCHQAY Name: Excel Opportunity_Lead__r_attributes_attributes: {'type': 'User', 'url': '/services/data/v46.0/sobjects/User/fdjf7r038'} Opportunity_Lead__r_attributes_Name: Vladimír Ivanov Opportunity_Lead__r_Name: Vladimír Ivanov Opportunity_Lead__c: 747fhdkdk CreatedDate: 2019-04-05T07:14:13.000+0000 Delivery_Start_Date__c: 2019-04-05 Delivery_End_Date__c: 2019-04-30 Amount: 3000.0 StageName: Won CloseDate: 2019-04-05 Opportunity_Owner__c: Petr Amir Owner_attributes_attributes: {'type': 'User', 'url': '/services/data/v46.0/sobjects/User/fhd6400037fh'} Owner_attributes_Cost_Centre_Code__c: FGHKD Owner_Cost_Centre_Code__c: FGHKD OpportunityLineItems_totalSize: 1 OpportunityLineItems_done: True OpportunityLineItems_records: [{'attributes': {'type': 'OpportunityLineItem', 'url': '/services/data/v46.0/sobjects/OpportunityLineItem/00k4I00001k7uxfQAA'}, 'ProductCode': 'TYDL0'}] Process finished with exit code 0
data = json.loads(user) rec = data['records'][0] new = {} def decompose(data, new): for k in data: if isinstance(data[k], dict): new.update(decompose(data[k], new)) elif isinstance(data[k], list): for i in data[k]: new.update(decompose(i,new)) else: value = data[k] while True: if k in new: k = f'*{k}' else: new[k] = value break return new for k, v in rec.items(): print(f'{k}: {v}') print('==============') new = decompose(rec,new) for k, v in new.items(): print(f'{k}: {v}')
attributes: {'type': 'Opportunity', 'url': '/services/data/v46.0/sobjects/Opportunity/0064I00000qtnCHQAY'} Id: 0064I00000qtnCHQAY Name: Excel Opportunity_Lead__r: {'attributes': {'type': 'User', 'url': '/services/data/v46.0/sobjects/User/fdjf7r038'}, 'Name': 'Vladimír Ivanov'} Opportunity_Lead__c: 747fhdkdk CreatedDate: 2019-04-05T07:14:13.000+0000 Delivery_Start_Date__c: 2019-04-05 Delivery_End_Date__c: 2019-04-30 Amount: 3000.0 StageName: Won CloseDate: 2019-04-05 Opportunity_Owner__c: Petr Amir Owner: {'attributes': {'type': 'User', 'url': '/services/data/v46.0/sobjects/User/fhd6400037fh'}, 'Cost_Centre_Code__c': 'FGHKD'} OpportunityLineItems: {'totalSize': 1, 'done': True, 'records': [{'attributes': {'type': 'OpportunityLineItem', 'url': '/services/data/v46.0/sobjects/OpportunityLineItem/00k4I00001k7uxfQAA'}, 'ProductCode': 'TYDL0'}]} ============== type: Opportunity url: /services/data/v46.0/sobjects/Opportunity/0064I00000qtnCHQAY Id: 0064I00000qtnCHQAY Name: Excel *type: User *url: /services/data/v46.0/sobjects/User/fdjf7r038 *Name: Vladimír Ivanov Opportunity_Lead__c: 747fhdkdk CreatedDate: 2019-04-05T07:14:13.000+0000 Delivery_Start_Date__c: 2019-04-05 Delivery_End_Date__c: 2019-04-30 Amount: 3000.0 StageName: Won CloseDate: 2019-04-05 Opportunity_Owner__c: Petr Amir **type: User **url: /services/data/v46.0/sobjects/User/fhd6400037fh Cost_Centre_Code__c: FGHKD totalSize: 1 done: True ***type: OpportunityLineItem ***url: /services/data/v46.0/sobjects/OpportunityLineItem/00k4I00001k7uxfQAA ProductCode: TYDL0 Process finished with exit code 0