0

How can I get the name of json object so that I can match on it in an if statement?

def test():
    device_config = api_get_conf()
    routing_protocol = device_config['Cisco-IOS-XE-native:native']
    if 'ospf' in routing_protocol:
        print('It worked!')
    else:
        print('dunno')

The routing_protocol variable has the following information in it:

        {"Cisco-IOS-XE-ospf:router-ospf": {
            "ospf": {
                "process-id": [
                    {
                        "id": 100,
                        "area": [
                            {
                                "area-id": 0,
                                "authentication": {
                                    "message-digest": [
                                        null
                                    ]
                                }
                            }
                        ],
                        "network": [
                            {
                                "ip": "192.168.200.200",
                                "wildcard": "0.0.0.0",
                                "area": 0
                            }
                        ]
                    }
                ]
            }
        }
    }

I would like to match on only 'Cisco-IOS-XE-ospf:router-ospf' or 'ospf'. Any help on how I can do this would be appreciated.

2
  • probably better to rephrase your question Commented Mar 30, 2020 at 0:05
  • Since there is only one element in your example, it seems to make little sense. Do you mean that there could be multiple and you only want to select those that match some (partial) key name? Or do you want to match the first if any? Are you essentially just asking how to select elements from a dictionary for which the key matches some regex? Commented Mar 30, 2020 at 0:09

2 Answers 2

1
def test():
    device_config = api_get_conf()
    # since we use get here, if we dont find it we set routing_protocol as False, easier to use on if
    routing_protocol = device_config.get('Cisco-IOS-XE-native:native', False)
    if routing_protocol:
        if 'ospf' in routing_protocol:
            print('It worked!')
        print("ospf not a key")
    else:
        print('routing protocol not a key')
Sign up to request clarification or add additional context in comments.

4 Comments

The get() function will return None if 'Cisco-IOS-XE-native:native' is not in the dictionary. As a result, the if 'ospf' in routing_protocol: method may throw an exception - let me modify your code.
@Mark yes Im aware of that, forgot to modify the if, thanks for the fix!
I am using json.loads in another python module I wrote and it creates a dict device_config which you see me pulling in at the top with device_config = api_get_conf(). With that said, I tried to add routing_protocol = device_config.get('Cisco-IOS-XE-native:native') and if routing_protocol and 'ospf' in routing_protocol: and it still doesn't match the if condition. Thanks for the help!
@Ant check it out, 2 "ifs" just for explaining but you can do it on one if .
0

I'm not sure what you mean, but it looks like api_get_conf() returns Dictionary where "Cisco-IOS-XE-ospf:router-ospf" is first key, and its value is another dictionary where key is "ospf". If it's what you wants to compare then you can simply use .keys() method on routing_protocol dict.

def test():
    device_config = api_get_conf()
    routing_protocol = device_config['Cisco-IOS-XE-native:native']
    if 'ospf' in routing_protocol.keys():
        print('It worked!')
    else:
        print('dunno')

2 Comments

Yeah that is exactly what I am trying to do. Sorry very new to python and json programming and trying to figure out how to get those values. When I add the .keys() it still doesn't pass the if condition.
After I tweaked it a bit, your answer was what I needed. Thanks!

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.