1

I've been looking for a few weeks and nowhere have i found anything that could help me with this specific problem.

I got a large output from an API call (Meraki) i'm looking to extract certain features out of the list.

Task: read output from API call, loop through output until status 'failed' is detected and print the interface and networkId of that item turn the networkId into a network name from a predefined list, and continue to print all "failed interfaces" till end of output.

The API call gets the entire organisation and i want to match the list of networkid's with Network names (since they aren't added in the same API call) so its readable what network has which interface that failed.

The output contains a lot of data , and i don't need all of those output values like IP, gateway, DNS, etc.

an example of the output from the API call:

{'networkId': 'A_1234567890', 'serial': 'A1B2-C3D4-E5F6', 'model': 'MX64', 'lastReportedAt': '2021-01-01T10:00:00Z', 'uplinks': [{'interface': 'wan1', 'status': 'active', 'ip': '192.168.1.2', 'gateway': '192.168.1.1', 'publicIp': '192.168.1.3', 'primaryDns': '8.8.8.8', 'secondaryDns': '8.8.4.4', 'ipAssignedBy': 'static'}, {'interface': 'wan2', 'status': 'ready', 'ip': '172.16.1.2', 'gateway': '172.16.1.1', 'publicIp': '172.16.1.3', 'primaryDns': '8.8.8.8', 'secondaryDns': '8.8.4.4', 'ipAssignedBy': 'static'}]}

This is one network of which there are 50 in this organisation i want to check the status of.

I'm pretty new to Python and I've tried using while loops to sift through the output to find the failed status but i cant output the whole network's information connected to it, I've looked at but most examples are using small predefined lists of separate words or numbers.

the API call im using: (found the template and modified where necessary to get a total list of all networks in my organisation)

import requests

url = "https://api.meraki.com/api/v1/organizations/{ORG_ID}/uplinks/statuses"

payload = None

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())
Juriaan
  • 13
  • 5

2 Answers2

0

Based on your sample output, looks like you have got the network ID only once in response and interface and is seen many times as part of Uplink attribute, Hence, you can parse the API response as a Json object and have the network names - network ID mapping in a dictionary and do something like below to get the failed status

net_names = {'A_1234567890':'abc', 'b':'xyz'}
network_id =response_json.get('networkId')
for item in response_json['uplinks']:
    if item['status'] == "failed":
        print('network ID:', network_id,'network_name:',net_names.get(network_id), 'Interface:',item['interface'])
Sid
  • 71
  • 1
  • 1
  • 9
  • AttributeError: 'Response' object has no attribute 'get' – Juriaan Oct 26 '21 at 13:36
  • you need to convert the response to JSON first 'response = json.loads(response)' – Sid Oct 27 '21 at 07:44
  • raise TypeError(f'the JSON object must be str, bytes or bytearray, ' TypeError: the JSON object must be str, bytes or bytearray, not Response @Sid still no cake. – Juriaan Oct 28 '21 at 08:05
0

Answer given in the another post, by @Szabolcs:

net_names = {"A_1234567890": "Name"}
for network_data in json_data:
  network_id = network_data.get("networkId")
  for uplink_data in network_data.get("uplinks", []):
    if uplink_data["status"] == "failed":
      print(
        "network ID:",
        network_id, ""
        "Network:",
        net_names.get(network_id, "n/a"),
        "- Interface:",
        uplink_data["interface"],
        "- failed",)

Does all i want.

CuriousSuperhero
  • 6,531
  • 4
  • 27
  • 50
Juriaan
  • 13
  • 5