+ 1

Would you shed some light on this Python/JSON question?

I am not able to make the print line to work beyond ["TABLE_intf"]. I'm trying to get the code to print the interface's information only (IP address, state (up or down), etc) from the attached JSON file. I'd appreciate any assistance, thanks. import requests import json url='http://xxx.cisco.com/ins' switchuser='xxx' switchpassword='xxx' myheaders={'content-type':'application/json'} payload={ "ins_api": { "version": "1.0", "type": "cli_show", "chunk": "0", "sid": "1", "input": "show ip interface brief", "output_format": "json" } } response = requests.post(url,data=json.dumps(payload), headers=myheaders,auth=(switchuser,switchpassword)).json() print(response["ins_api"]["outputs"]["output"]["body"]["TABLE_intf"])#["ROW_intf"]["prefix"]) I'll paste the JSON code in another post.. https://www.sololearn.com/discuss/2215491/?ref=app

27th Mar 2020, 5:20 AM
Jairo Arce Hernandez
Jairo Arce Hernandez - avatar
3 Answers
+ 1
["Table_int" is actually a dictionary of another dictionary, "Row_intf" which is also a dictionary, containing a list. Since lists can't be accessed with keys-values method, I think you should use index. so continue from this line ["Table_intf]"](["Row_intf"][0]).get("prefix")
27th Mar 2020, 6:01 AM
Mirielle
Mirielle - avatar
+ 1
Code Crasher, this code is from a (Cisco) Nexus switch API. You might want to play around with some APIs and sandbox es at developer.cisci.com which are free and Postman, if this is interesting to you
27th Mar 2020, 6:09 AM
Jairo Arce Hernandez
Jairo Arce Hernandez - avatar
0
Thanks Mireille! I'll give it a try tomorrow
27th Mar 2020, 6:05 AM
Jairo Arce Hernandez
Jairo Arce Hernandez - avatar