How to parse JSON data from API response in Python?

22,962

Like @danil-kondratiev said, you can use response.json() and you don't need to encode/decode. Will this work for you?

import requests

response = requests.get('url with keys')

json_data = response.json() if response and response.status_code == 200 else None

if json_data and 'hoststatuslist' in json_data:
    if 'hoststatus' in json_data['hoststatuslist']:
        for hoststatus in json_data['hoststatuslist']['hoststatus']:
            host_name = hoststatus.get('name')
            status_text = hoststatus.get('status_text')
Share:
22,962

Related videos on Youtube

Wish I Knew this stuff
Author by

Wish I Knew this stuff

Updated on April 13, 2020

Comments

  • Wish I Knew this stuff
    Wish I Knew this stuff about 4 years

    I am trying to write a script that will pull current status from our monitoring tools and update them in MS SQL DB. When I call the API I get a HUGE response in JSON format:

    {
      "hoststatuslist": {
        "recordcount": "1084",
        "hoststatus": [
          {
            "@attributes": {
              "id": "XXXX"
            },
            "host_id": "XXX",
            "name": "XXXXX",
            "display_name": "XXXXXXX",
            "address": "XXXXXX",
            "alias": "XXXXXX",
            "status_text": "XXXXXXXXXXXXXXXXXXXXXXX",
            etc.
          },
          {
            "@attributes": {
              "id": "XXXX"
            },
            "host_id": "XXX",
            "name": "XXXXX",
            "display_name": "XXXXXXX",
            "address": "XXXXXX",
            "alias": "XXXXXX",
            "status_text": "XXXXXXXXXXXXXXXXXXXXXXX",
            etc.
          },
          etc.
        ]
      }
    }
    

    As you can see I get over 1000 host objects with attributes. I want to parse the response, so that I can add/update the MS SQL DB. I'm trying to parse out the host_id, name, and status_text for each host.

    I tried to do something like this Python - Parsing JSON Data Set but I keep getting errors that the response object has no attribute read or decode.

    Here is my current code:

    import requests
    import json
    
    response = requests.get('url with API Key')
    decoded_response = response.read().decode("UTF-8")
    data = json.loads(decoded_response)
    jsonData = data["hoststatus"]
    
    for host in jsonData:
        Name = host.get("name")
        StatusText = host.get("status_text")
    

    If anyone has a suggestion to do this with another language or tool I am open. I need to call about 20 APIs and put all the status/other information into a DB so that it's all in one location.

    Any help is appreciated.

  • Wish I Knew this stuff
    Wish I Knew this stuff almost 7 years
    I'm not sure if I didn't explain myself correctly or if I misunderstand your post, but this post is simple giving me the json data. I already have the data I need to pull out the Host name/status from the json data. As a side note when I try r.encoding I receive no response even with a 200 status code