0

Trying to loop from API response need help with nested JSON

I only have a few months of learning python so I don't have much experience. I'm trying to get results of a API response so I can make another API call. I need to get the portID and system name so I can make the 2nd API call I want. I only need the LLDP information since that has the information I need. Below I have the first two API calls that I have come up with. I'm having issues with the lldp_results. Since it's nested I don't know how to loop through it. Below is the script that I have. import pprint import meraki API_KEY = KEY dashboard = meraki.DashboardAPI(API_KEY) organization_id = 'ORG-ID' response = dashboard.organizations.getOrganizationInventoryDevices( organization_id, total_pages='all', productTypes='switch' ) for switch in response: lldp_results = dashboard.devices.getDeviceLldpCdp( switch['serial'] ) pprint.pprint(lldp_results) This is the lldp_results information [{'ports': {'1': {'lldp': {'portId': 'c8:d9:d2:cc:bb:aa', 'sourcePort': '1'}}, '12': {'lldp': {'portId': '50:a4:d0:cc:bb:aa', 'sourcePort': '12'}}, '13': {'lldp': {'portId': '48:21:0b:cc:bb:aa', 'sourcePort': '13'}}, '14': {'lldp': {'portId': '50:a4:d0:cc:bb:aa', 'sourcePort': '14'}}, '15': {'lldp': {'portId': '48:21:0b:cc:bb:aa', 'sourcePort': '15'}}, '3': {'lldp': {'portId': '00:07:32:cc:bb:aa', 'sourcePort': '3'}}, '37': {'cdp': {'address': '10.60.138.82', 'deviceId': 'AABBCCDDEEFF', 'portId': 'Port 1', 'sourcePort': '37'}, 'lldp': {'managementAddress': '10.60.138.82', 'portId': 'AABBCCDDEEFF:P1', 'sourcePort': '37', 'systemName': 'AABBCCDDEEFF'}}, '4': {'lldp': {'portId': '50:a4:d0:92:bb:aa', 'sourcePort': '4'}}, '43': {'cdp': {'address': '10.60.138.81',

15th Apr 2022, 9:58 PM
Mr Assault
Mr Assault - avatar
6 ответов
+ 1
Try if this works: import json llsp_json = json.loads(lldp_results) for port in llsp_json["ports"]: print(llsp_json["ports"][n])
15th Apr 2022, 11:10 PM
Simon Sauter
Simon Sauter - avatar
+ 1
Oh, sorry. I mixed up the name of the loop variable. Try if this works: import json llsp_json = json.loads(lldp_results) for port in llsp_json["ports"]: print(llsp_json["ports"][port])
16th Apr 2022, 9:10 AM
Simon Sauter
Simon Sauter - avatar
+ 1
The advantage of using the json module is that it allows you to access elements by position, i.e. without knowing the index.
16th Apr 2022, 9:12 AM
Simon Sauter
Simon Sauter - avatar
0
I tried doing this: lldp_results['ports']['43']['lldp'] and get {'sourcePort': '43', 'systemName': 'Meraki MR70 - UN1212INDWAP01', 'portId': '0'} But I don't know how to loop through the numbers after Ports since the numbers change
15th Apr 2022, 9:59 PM
Mr Assault
Mr Assault - avatar
0
@Simon Sauter I added the import json I tried your suggestion and first got an error when I tried the json.loads. There erros is: TypeError: the JSON object must be str, bytes or bytearray, not dict after somg google I then did a lldp_temp = json.dumps(lldp_results) then lldp_json = json.loads That worked and then I tried with the loop and get this error NameError Traceback (most recent call last) Input In [29], in <cell line: 1>() 1 for ports in lldp_json['ports']: ----> 2 print(lldp_json['ports'][n]) NameError: name 'n' is not defined If I try to print(lldp_json['ports'] this works with no issues
16th Apr 2022, 2:09 AM
Mr Assault
Mr Assault - avatar
0
i just went around my problem by creating a object in a loop that contains all the port numbers (12,13,14 …)
16th Apr 2022, 6:50 AM
Mr Assault
Mr Assault - avatar