Access a list nested inside a dictionary value

  meraki-api, python, python-3.x

I have the following Meraki API reply for a GET request

[{'networkId': 'L_6687845446656402', 'serial': 'Q2HY-MQC2-EFSW', 'uplink': 'wan2', 'ip': '8.8.8.8', 'timeSeries': [{'ts': '2021-11-25T19:54:13Z', 'lossPercent': None, 'latencyMs': None}, {'ts': '2021-11-25T19:55:13Z', 'lossPercent': None, 'latencyMs': None}, {'ts': '2021-11-25T19:56:13Z', 'lossPercent': None, 'latencyMs': None}, {'ts': '2021-11-25T19:57:13Z', 'lossPercent': None, 'latencyMs': None}, {'ts': '2021-11-25T19:58:13Z', 'lossPercent': None, 'latencyMs': None}, ....]}]

lossPercent is a key that takes a float or a bool value and is nested inside a list in a dictionary

I am trying to write a code to find lossPercent value> 5.00 in response_data and run:

import requests    
response = requests.request('GET', dev_status, headers=headers, data = payload)
response_data = json.loads(response.text)
    
    for i in range(len(response_data)):
        for p in response_data[i]['timeSeries'].values: # This is where I am stuck
            if p >5:
                try:
                    f = open("PL.txt", "a")
                    print("Serial:" + str(response_data[i]['serial']), file=f)
                    print("Uplink" + str(response_data[i]['uplink']), file=f)
                    print("IP:" + str(response_data[i]['ip']), file=f)
                    print("Time Series:" + str(response_data[i]['timeSeries']), file=f)
                    print('n', file=f)
                    f.close()
                except KeyError:
                    continue

I am not sure how to proceed

Source: Python-3x Questions

LEAVE A COMMENT