I am trying to breakout the "Records Section" from the response. Is this possible? Below is the code I have currently. It returns the MAC and the whole record section.
def find_device():
clearconsole()
API_KEY = get_meraki_api_key()
dashboard = meraki.DashboardAPI(API_KEY, suppress_logging=True)
organization_id = '412637'
mac = input ('Which device are you trying to find (xx:xx:xx:xx:xx:xx): ')
response = dashboard.organizations.getOrganizationClientsSearch(
organization_id, mac, total_pages='all'
)
fields = ['mac','records']
for field in fields:
print ('\n' + 'Here is what I found: ' + '\n')
print (f"{field}: {response[field]}")
Solved! Go to solution.
The records section in the API response is a list of nested dictionary which you have to parse to retrieve the keys you need.
For example the structure of this API response is something like this -
API response = { key:value, record:{ [ { key1:value1, key2:value2, etc.} ] } }
In order to access the inner values of this nested dictionary you can do
record = response['records']
print ('\n' + 'Here is what I found: ' + '\n')
print("IP is ", record[0]['ip'])
print("SwitchPort is ", record[0]['switchport'])
print("Status is ", record[0]['status'])
You can learn about nested dictionaries here
I'm not sure, but I think it should be more like:
mac =response['mac']
records = response['records']
for record in records:
...
I will give this a try and let you know. Thanks
If you want just the MAC address of the client, you don't need to run the for loop printing all the response fields. Just do -
print ('\n' + 'Here is what I found: ' + '\n')
print ("mac is", response['mac'])
Yeah I can get the MAC to display but I want to pull certain values out of the records section of the data. Lets say I want to show the following:
MAC is fine but the other are part of the Records section.
The records section in the API response is a list of nested dictionary which you have to parse to retrieve the keys you need.
For example the structure of this API response is something like this -
API response = { key:value, record:{ [ { key1:value1, key2:value2, etc.} ] } }
In order to access the inner values of this nested dictionary you can do
record = response['records']
print ('\n' + 'Here is what I found: ' + '\n')
print("IP is ", record[0]['ip'])
print("SwitchPort is ", record[0]['switchport'])
print("Status is ", record[0]['status'])
You can learn about nested dictionaries here
Thank you this works perfect and now know how to get that info out.