Get Organization Clients Search Breakout "Records" Section

Solved
eroche
Getting noticed

Get Organization Clients Search Breakout "Records" Section

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]}")

 

1 Accepted Solution
shwetaP
Meraki Employee
Meraki Employee

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 

View solution in original post

6 Replies 6
PhilipDAth
Kind of a big deal
Kind of a big deal

I'm not sure, but I think it should be more like:

 

mac =response['mac']
records = response['records']

for record in records:
  ...
eroche
Getting noticed

I will give this a try and let you know. Thanks

shwetaP
Meraki Employee
Meraki Employee

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'])

 

eroche
Getting noticed

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
  • IP
  • Switchport
  • Status

 

MAC is fine but the other are part of the Records section.

shwetaP
Meraki Employee
Meraki Employee

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 

eroche
Getting noticed

Thank you this works perfect and now know how to get that info out.

Get notified when there are additional replies to this discussion.