Home > Blockchain >  How to grab value in data from json resonse?
How to grab value in data from json resonse?

Time:10-29

Q: how do i grab the value's from my api's response?

my code:

#!/usr/bin/python3

API_KEY = ""
ORG_ID = ""

import meraki
import requests
import json
import time
import sys
from pprint import pprint

url = "https://api.meraki.com/api/v1/organizations/""/appliance/uplink/statuses"

payload = {}
headers = {
  'Content-Type': 'application/json',
  'Accept': 'application/json',
  'X-Cisco-Meraki-API-Key': (API_KEY)
}

response = requests.request('GET', url, headers=headers, data = payload)
pprint(response.json())

the response:

{'networkId': 'A_1234567890', 
'serial': 'abcd-1234-abcd', 
'model': 'MXYZ', 
'lastReportedAt': '2021-01-01T00:00:00Z', 
'uplinks': [{'interface': 'wan1', 
             'status': 'active', 
             'ip': 'x.x.x.x', 
             'gateway': 'x.x.x.x', 
             'publicIp': 'x.x.x.x', 
             'primaryDns': 'x.x.x.x', 
             'secondaryDns': 'x.x.x.x', 
             'ipAssignedBy': 'dhcp'}, 
             {'interface': 'wan2', 
             'status': 'ready', 
             'ip': 'x.x.x.x', 
             'gateway': 'x.x.x.x', 
             'publicIp': 'x.x.x.x', 
             'primaryDns': 'x.x.x.x', 
             'secondaryDns': 'x.x.x.x', 
             'ipAssignedBy': 'dhcp'}]}, 

{'networkId': 'B_0987654321', 
'serial': '1234-abcd-1234', 
'model': 'MXYZ', 
'lastReportedAt': '2021-01-01T00:00:00Z', 
'uplinks': [{'interface': 'wan1', 
             'status': 'active', 
             'ip': 'x.x.x.x', 
             'gateway': 'x.x.x.x', 
             'publicIp': 'x.x.x.x', 
             'primaryDns': 'x.x.x.x', 
             'secondaryDns': 'x.x.x.x', 
             'ipAssignedBy': 'dhcp'}, 
             {'interface': 'wan2', 
             'status': 'failed', 
             'ip': 'x.x.x.x', 
             'gateway': 'x.x.x.x', 
             'publicIp': 'x.x.x.x', 
             'primaryDns': 'x.x.x.x', 
             'secondaryDns': 'x.x.x.x', 
             'ipAssignedBy': 'dhcp'}]}, 

These are 2 from the entire list of 50 networks.

What i want it to do is:

find the status "failed", and print the connection error, and where i can find it like this:

['networkId: XYZ', 'network name: ABC', 'interface: wan1', 'uplinks: failed'] or something along those lines to make it readable.

Additionally i've gotten this bit of code to create a network name from networkId and search for uplinks and but i can't make it work.

net_names = {
'A_1234567890':'Amsterdam', 
'B_0987654321':'Rotterdam'
}
network_id = response_json.get('networkId')
for item in response_json['uplinks']:
    if item['status'] == "active":
        print('network ID:', network_id,'network_name:',net_names.get(network_id), 'Interface:',item['interface'])

i'm pretty new to Python and i read a lot about JSON, but its all on dicts and lists and it doesn't make much sense to me, to help me out in my specific problem.

Any help is greatly appreciated.

CodePudding user response:

Try this:

json_data = response.json()

net_names = {"A_1234567890": "Amsterdam", "B_0987654321": "Rotterdam"}
for network_data in json_data:
    network_id = network_data.get("networkId")
    for uplink_data in network_data.get("uplinks", []):
        if uplink_data["status"] == "active":
            print(
                "network ID:",
                network_id,
                "network_name:",
                net_names.get(network_id, "n/a"),
                "Interface:",
                uplink_data["interface"],
            )
  • Related