0

I'm getting a JSON data from RESTCONF HTTPS request, using the following code.

https_request = 'https://' + host + '/restconf/data/Cisco-IOS-XE-native:native/interface/'
headers = {'Content-type': 'application/yang-data+json', 'Accept': 'application/yang-data+json'}
r = requests.get(https_request, auth=(user, password), headers=headers, verify=False)
print r.json()

The JSON file I got:

{
"Cisco-IOS-XE-native:interface": {
    "GigabitEthernet": [
        {
            "name": "1",
            "description": "DON'T TOUCH ME",
            "isis": {
                "Cisco-IOS-XE-isis:metric": {
                    "value": 2
             }
            ....
        },
        {
            "name": "2",
            "isis": {
                "Cisco-IOS-XE-isis:metric": {
                    "value": 4
                } ....

        },
        {
            "name": "3",
            "shutdown": [
                null
            ],
            "isis": {
                "Cisco-IOS-XE-isis:metric": {
                    "value": 7
                }....                
        }
    ],
    "Loopback": [
        {
            "name": 0,
            "isis": {
                "Cisco-IOS-XE-isis:metric": {
                    "value": 1
                }               
    ],
    "Tunnel": [
        {
            "name": 0,
            "isis": {
                "Cisco-IOS-XE-isis:metric": {
                    "value": 3
                }....
            }
    ]
}

Basically, I want my function to return the field's "value" of isis of each interface. I tried the following code for GigabitEthernet:

value = r.json()['Cisco-IOS-XE-native:interface']['GigabitEthernet'][0]['isis']['metric']['value']

I got this error:

print Router_1.get_isis_metric()['Cisco-IOS-XE-native:interface']['GigabitEthernet'][0]['isis']['metric']['isis']
KeyError: 'metric'
0

2 Answers 2

5

I think, you have misspelled metric for Cisco-IOS-XE-isis:metric

Try :

value = r.json()['Cisco-IOS-XE-native:interface']['GigabitEthernet'][0]['isis']['Cisco-IOS-XE-isis:metric']['value']

Edit 1

for index in range(len(r.json()['Cisco-IOS-XE-native:interface']['GigabitEthernet'])):
    value = r.json()['Cisco-IOS-XE-native:interface']['GigabitEthernet'][index]['isis']['Cisco-IOS-XE-isis:metric']['value']
    print(value)
Sign up to request clarification or add additional context in comments.

4 Comments

I think you've slightly mixed up the order of selectors. It should be ...['isis']['Cisco-IOS-XE-isis:metric']... but you have them the other way round
OP wants the value of isis for each interface. You only solved the KeyError exception
@DanScally : You are correct.. modified.. thanks. .:)
@Eraw : I have added that too in Edit section.. Please check
3

Using a list comprehension, where you iterate over the list of dictionaries and collect the value for each interface, example for GigabitEthernet


dct = {
"Cisco-IOS-XE-native:interface": {
    "GigabitEthernet": [
        {
            "name": "1",
            "description": "DON'T TOUCH ME",
            "isis": {
                "Cisco-IOS-XE-isis:metric": {
                    "value": 2
             }}

        },
        {
            "name": "2",
            "isis": {
                "Cisco-IOS-XE-isis:metric": {
                    "value": 4
                }}

        },
        {
            "name": "3",
            "shutdown": [
                None
            ],
            "isis": {
                "Cisco-IOS-XE-isis:metric": {
                    "value": 7
                }}
        }
    ]}}

result = [item['isis']["Cisco-IOS-XE-isis:metric"]['value'] for item in dct['Cisco-IOS-XE-native:interface']['GigabitEthernet']]

The output will be

[2, 4, 7]

Or to collect values for all interfaces, you can loop over the interfaces and collect the value for each interface

interfaces = ['GigabitEthernet', 'Loopback', 'Tunnel']
result = [item['isis']["Cisco-IOS-XE-isis:metric"]['value'] for interface in interfaces for item in dct['Cisco-IOS-XE-native:interface'][interface]]
print(result)

The output will be

[2, 4, 7, 1, 3]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.