Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extrating Json response PYTHON

I need help my friends, it’s my first project with Api, and I’m not able to pull the data from the Api, it returns the following error.

Script:

response = data.json()

for number in response['mobile_phones']:
    dd = resposta['ddd']
    num = resposta['number']
    whatsapp = resposta['whatsapp_datetime']
    print(dd+num+whatsapp)

Erro:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Erro: KeyError: 'mobile_phones'

Response Api

{
    "cpf": 52289257591,
    "mobile_phones": [
      {
        "ddd": 27,
        "number": "999111151",
        "priority": 1,
        "cdr_datetime": null,
        "hot_datetime": null,
        "whatsapp_datetime": "2022-03-05 00:00:00",
        "cpc_datetime": null
      },
      {
        "ddd": 27,
        "number": "998608229",
        "priority": 2,
        "cdr_datetime": null,
        "hot_datetime": null,
        "whatsapp_datetime": "2022-03-07 00:00:00",
        "cpc_datetime": null
      },
      {
        "ddd": 27,
        "number": "992250660",
        "priority": 3,
        "cdr_datetime": null,
        "hot_datetime": null,
        "whatsapp_datetime": "2022-03-12 00:00:00",
        "cpc_datetime": null
      }
    ],
    "ip": "135.199.5.98",
    "plan": "Consulta simples"
  }
]

>Solution :

You can do this,

for number in response[0]['mobile_phones']:
    dd = number['ddd']
    num = number['number']
    whatsapp = number['whatsapp_datetime']
    print(dd, num, whatsapp)

response is a list, which has dictionary of values.

The list you are looking its need to access like this, response[0]['mobile_phones']

Output:

27 999111151 2022-03-05 00:00:00
27 998608229 2022-03-07 00:00:00
27 992250660 2022-03-12 00:00:00
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading