Get Networks and Only print certain fields

Solved
eroche
Getting noticed

Get Networks and Only print certain fields

Hi All

 

Looks to pull all my networks but only want the id and name to be printed. Here is what I have:

 

import meraki
from pprint import pprint

# Defining your API key as a variable in source code is discouraged.
# This API key is for a read-only docs-specific environment.
# In your own code, use an environment variable as shown under the Usage section
# @ https://github.com/meraki/dashboard-api-python/

API_KEY = ''

dashboard = meraki.DashboardAPI(API_KEY, suppress_logging=True)

organization_id = ''

response = dashboard.organizations.getOrganizationNetworks(
organization_id, total_pages='all'
)

fields = [id, name]

for field in fields:
pprint (f"{field}: {response[field]}")

 

 

 

Suggestions...keep getting error about the fields = [id, name]

Traceback (most recent call last):
File "/mnt/m2ssd/python/meraki_get_network_id.py", line 19, in <module>
fields = [id, name]
^^^^
NameError: name 'name' is not defined

1 Accepted Solution
alemabrahao
Kind of a big deal
Kind of a big deal

Don't change the value in RED.

 

fields = ['id', 'name']
for item in response:
for field in fields:
pprint(f"{field}: {item[field]}")

I am not a Cisco Meraki employee. My suggestions are based on documentation of Meraki best practices and day-to-day experience.

Please, if this post was useful, leave your kudos and mark it as solved.

View solution in original post

7 Replies 7
alemabrahao
Kind of a big deal
Kind of a big deal

The error you’re seeing is because id and name are not defined as strings in your list. In Python, strings should be enclosed in quotes.

 

fields = ['id', 'name']

I am not a Cisco Meraki employee. My suggestions are based on documentation of Meraki best practices and day-to-day experience.

Please, if this post was useful, leave your kudos and mark it as solved.
eroche
Getting noticed

ok so when I do that I get

 

Traceback (most recent call last):
File "/mnt/m2ssd/python/meraki_get_network_id.py", line 22, in <module>
pprint (f"{field}: {response[field]}")
~~~~~~~~^^^^^^^
TypeError: list indices must be integers or slices, not str

alemabrahao
Kind of a big deal
Kind of a big deal

In Python, lists can only be indexed by integers or slices, not strings.

 

for item in response:
  for field in fields:
pprint(f"{field}: {item[field]}")

I am not a Cisco Meraki employee. My suggestions are based on documentation of Meraki best practices and day-to-day experience.

Please, if this post was useful, leave your kudos and mark it as solved.
eroche
Getting noticed


fields = ['id', 'name']
for item in response:
    for field in fields:
         pprint (f"{field}: {response[field]}")

 

It wanted the pprint indented to here but still have this error

 

Traceback (most recent call last):
File "/mnt/m2ssd/python/meraki_get_network_id.py", line 22, in <module>
pprint (f"{field}: {response[field]}")
~~~~~~~~^^^^^^^
TypeError: list indices must be integers or slices, not str

alemabrahao
Kind of a big deal
Kind of a big deal

Don't change the value in RED.

 

fields = ['id', 'name']
for item in response:
for field in fields:
pprint(f"{field}: {item[field]}")

I am not a Cisco Meraki employee. My suggestions are based on documentation of Meraki best practices and day-to-day experience.

Please, if this post was useful, leave your kudos and mark it as solved.
eroche
Getting noticed

You the man thank you much appricated!

Jamieinbox
Building a reputation

Try this starter code and change it any way you want. It displays your network name and ID.

Merakicode/DisplayNetworkStarterCode at main · jadexing/Merakicode · GitHub

 

Get notified when there are additional replies to this discussion.