如何在python中读取json中的json?

问题描述 投票:0回答:1

我想读取一个json结果,但是我不知道如何在json里面读取json。我用pandas中的 "json_nomalize "来读取一部分结果,但是我无法读取联系人中的字段,如:姓名、邮箱、电话、手机、手机标题等。

这是json的结果。

{'totalSize': 1295,
 'done': True,
 'records': [{'attributes': {'type': 'Account',
    'url': '/services/data/v48.0/sobjects/Account/xxxxxxxxx'},
   'Id': 'xxxxxxxxx',
   'Name': 'Empresa Teste_Mapeamento',
   'BillingStreet': 'Teste, 950',
   'BillingCity': 'São Paulo',
   'BillingState': 'SP',
   'BillingPostalCode': '00000-002',
   'CPF_ou_CNPJ__c': None,
   'Contacts': {'totalSize': 1,
    'done': True,
    'records': [{'attributes': {'type': 'Contact',
       'url': '/services/data/v48.0/sobjects/Contact/xxxxxxxxx'},
      'Name': 'Teste_Testes',
      'Email': '[email protected]',
      'Phone': '11 00000000',
      'MobilePhone': '11 00000000',
      'Title': None}]}},

这是我做的

r = r.json()
df = pd.json_normalize(r['records'])

请在此输入图片描述

结果,我需要一个包含这些字段的数据框架。

Id   Name   BillingStreet   BillingCity   BillingState   BillingPostalCode   CPF_ou_CNPJ__c   Contacts.Name   Contacts.Email   Contacts.Phone   Contacts.MobilePhone   Contacts.Title
python json pandas normalize
1个回答
0
投票

你可以试试这个。

df = pd.json_normalize(mydict, 
                       record_path=['records', 'Contacts', 'records'], 
                       meta=[['totalSize'], ['done'], ['records', 'Id'], ['records', 'Name']])

输出:

           Name               Email        Phone  MobilePhone Title attributes.type                                   attributes.url totalSize  done records.Id              records.Name
0  Teste_Testes  [email protected]  11 00000000  11 00000000  None         Contact  /services/data/v48.0/sobjects/Contact/xxxxxxxxx      1295  True  xxxxxxxxx  Empresa Teste_Mapeamento
© www.soinside.com 2019 - 2024. All rights reserved.