08-28-2016 06:08 PM - edited 03-01-2019 04:30 AM
I'm beginner and trying to write python to understand this more. if I remove the # from print I can see the nice long out put and I want to further code it to get only few items from that output. When I run this code I get error "TypeError: string indices must be integers"
this is my first post on forum so not sure if I can get help on this code or not, but any input to help me to fix the error and provide some education would be great !
import requests
import json
import sys
requests.packages.urllib3.disable_warnings()
url = "https://devnetapi.cisco.com/sandbox/apic_em/api/v1/ticket"
payload = {"username":"devnetuser","password":"Cisco123!"}
header = {"content-type":"application/json"}
output = requests.post(url,data=json.dumps(payload),headers=header,verify=False)
response_json = output.json()
ST = response_json['response']['serviceTicket']
headers = {"X-Auth-Token": ST}
ip = "https://devnetapi.cisco.com/sandbox/apic_em/api/v1/network-device"
response1 = requests.get(ip,headers=headers,verify=False)
network_Devices = response1.json()
#print(json.dumps(network_Devices,indent=4))
devices_list = []
for item in network_Devices:
devices_list.append([item["hostname"],item["managementIpAddress"],item["type"],item["id"]])
print (devices_list)
08-28-2016 06:39 PM
The response JSON from requests.get(ip,headers=headers,verify=False) is
{
"response": [
{
"apManagerInterfaceIp": "",
"type": "Cisco Catalyst 2960C-8PC-L Switch",
"upTime": "1 day, 2:20:31.50",
"locationName": null,
"memorySize": "59556224",
"lastUpdateTime": 1467837623914,
We actually just need everything inside "response":[
....
So we use "network_Devices = response1.json()["response"]" instead.
print(json.dumps(response1.json(),indent=4))
network_Devices = response1.json()["response"]
print(json.dumps(network_Devices,indent=4))
devices_list = []
for item in network_Devices:
devices_list.append([item["hostname"],item["managementIpAddress"],item["type"],item["id"]])
print (devices_list)
To print network device list in pretty format please refer to this learning lab session
08-29-2016 05:12 AM
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide