Help with Python dictionary Queries from both Xml &Json

Hello all. I’m a total newby so please bear with me. i have this xml file which has been parsed and I’m using xmltodict here is the file.
here is the xml

pprint(xml_example)
(‘<?xml version="1.0" encoding="UTF-8" ?>\n’
‘\n’
’ GigabitEthernet2\n’
’ Wide Area Network\n’
’ true\n’
’ \n’
’ \n’
’ 172.16.0.2\n’
’ 255.255.255.0\n’
’ \n’
’ \n’
‘\n’)

Here is the query to generate an ip address notice how it is in the order of the info top down from the xml

test_address = xml_dict[“interface”][“ipv4”][“address”][“ip”]

Here is the query ran

test_address
‘172.16.0.2’

HOW DO I GET THE QUERY TO RETURN JUST THE IP ADDRESS AND SUBNET ?
the nearest i can get to it is

test10 = xml_dict[“interface”][“ipv4”][“address”]
test10
OrderedDict([(‘ip’, ‘172.16.0.2’), (‘netmask’, ‘255.255.255.0’)])

HOW CAN I GET IT JUST 172.16.0.0 255.255.255.0 ?
@@@@@@@@@@@@@@@@@@@@@@@@@@@@

The same query this time in json. again the only output i want is 172.16.0.0 255.255.255.0
Here is my original json

pprint(json_example)
(‘{\n’
’ “ietf-interfaces:interface”: {\n’
’ “name”: “GigabitEthernet2”,\n’
’ “description”: “Wide Area Network”,\n’
’ “enabled”: true,\n’
’ “ietf-ip:ipv4”: {\n’
’ “address”: [\n’
’ {\n’
’ “ip”: “172.16.0.2”,\n’
’ “netmask”: “255.255.255.0”\n’
’ }\n’
’ ]\n’
’ }\n’
’ }\n’
‘}\n’)

HERE IS MY QUERY

test1 = json_python[“ietf-interfaces:interface”][“ietf-ip:ipv4”][“address”]
test1
[{‘ip’: ‘172.16.0.2’, ‘netmask’: ‘255.255.255.0’}]

HOW DO I GET JUST 172.16.0.2 255.255.255.0 ?

1 Like
>>> test1
[{'ip': '172.16.0.2', 'netmask': '255.255.255.0'}]
>>> test1[0]["ip"]
'172.16.0.2'
>>> print(test1[0]["ip"])
172.16.0.2
>>> print(test1[0]["netmask"])
255.255.255.0

Explanation: The print tells you how to construct the values. The outer braces [] tell me it is a list. The list has one item, numbered 0. That one item is a dictionary, the braces being {}.

So the IP is found by first indexing with zero (the first item in the list) and then “ip”, the key of the ip-number.

xml and json are text formats, so you can paste these as formatted text without pretty printing. Please do so, because I had to “fix” the format to be able to use the texts. Also the shouting (all caps text) is generally considered overkill, please don’t.

1 Like

Hello and thanks for your help. Apologies for the caps, point noted. the reason for the pprint is i’m follwing a learning exercise and this is what they use.
What i’m actually trying to achieve is a single query from the xml that gives the answer as
test1
172.16.0.2 255.255.255.0 ?
How do i achieve this please?

1 Like

I do not understand your problem. You know how to get the individual elements, what is stopping you from putting these in order to get the desired result?

>>> test1 = [{ "ip": "172.16.0.2", "netmask": "255.255.255.0"}]
>>> print(test1[0]["ip"], test1[0]["netmask"])
172.16.0.2 255.255.255.0
1 Like

Thanks again sir. here is a copy of the slide where ultimately in his example he gets the output of the
int_name
‘GigabitEthernet2’

I was trying to apply the same logic to show me how to get an ip address and a subnet mask on the same line
test1
‘172.16.0.2 255.255.255.0’
i dont want to run two print commands i want to use one name, in this case test1 to show my the ip and subnet mask without the brackets. How or is this possible please?

1 Like

Than you need to have one variable, containing the string ‘172.16.0.2 255.255.255.0’. You can create it in several ways, it is just string handling. What did you try and how did it fail?

1 Like

I diddn’t fail per se. the nearest i could get to what i wanted was this

test8 = xml_dict[“interface”][“ipv4”][“address”]
test8
OrderedDict([(‘ip’, ‘172.16.0.2’), (‘netmask’, ‘255.255.255.0’)])

This returned
OrderedDict([(‘ip’, ‘172.16.0.2’), (‘netmask’, ‘255.255.255.0’)])

however all i wanted was 172.16.0.2 255.255.255.0 with none of the brackets etc

I understand now that i have to create one variable from the two strings. it makes sense now, many thanks.