-3
"fwt-master2": {
        "ipv4": {
                "rtr": {
                        "ip": "1.2.3.4",
                        "net": "3.4.5.6",
                        "netlen": "24",
                        "netmask": "255.255.255.0",
                        "broadcast": "7.8.9.1"
                }
        }

I am trying to get the ip value from this JSON file without specifying the values of each element (without using fwt-maser2[ipv4][rtr][ip]). using the .values() method (.values()[0].values()[0].values()[0]`)

I am getting the netlen value (24) instead of the ip values which is actually the first element.

why is such thing happening?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
alixander
  • 426
  • 1
  • 7
  • 18
  • "without using fwt-maser2[ipv4][rtr][ip]" **Why?** If you set such an arbitrary restriction, at least provide an explanation. Otherwise this smells like an XY problem. – DeepSpace Feb 11 '18 at 14:45
  • 4
    Python dictionaries *have no set order*. You can't rely on what element is going to be the first. Don't use `dict.values()` here, use keys. – Martijn Pieters Feb 11 '18 at 14:47
  • Dictionaries don't have "order". The "zero-th" element in the current execution may be the last in the next execution. – DeepSpace Feb 11 '18 at 14:47
  • if i had two second level keys, how can i get the value of a specific one of them ? – alixander Feb 11 '18 at 14:52

3 Answers3

-1

I think use of nested code to find key value is the best way ...this way you just search if "broadcast" key in dict then print its value Try something rom here: find all occurances of a key

Ujjawal Khare
  • 756
  • 2
  • 7
  • 20
-1

If you only know that the target key is "ip", then you can use recursion:

s = {"fwt-master2": {
    "ipv4": {
            "rtr": {
                    "ip": "1.2.3.4",
                    "net": "3.4.5.6",
                    "netlen": "24",
                    "netmask": "255.255.255.0",
                    "broadcast": "7.8.9.1"
            }
    }
  }
}

def get_ip(d):
   return [i for c in filter(None, [b if a == 'ip' else get_ip(b) if isinstance(b, dict) else None for a, b in d.items()]) for i in c]

print(''.join(get_ip(s)))

Output:

1.2.3.4
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
-2

I decided to go through your dictionary and found that its incomplete !!!

your dictionary :

 "fwt-master2": {
            "ipv4": {
                    "rtr": {
                            "ip": "1.2.3.4",
                            "net": "3.4.5.6",
                            "netlen": "24",
                            "netmask": "255.255.255.0",
                            "broadcast": "7.8.9.1"
                    }
            }

Actually it should be like : Added missing curly braces ... first and last two..

{"fwt-master2": { "ipv4": { "rtr": { "ip": "1.2.3.4", "net": "3.4.5.6", "netlen": "24", "netmask": "255.255.255.0", "broadcast": "7.8.9.1" }}}}

Well it happens ... so Amusing above updated one is the actual dictionary so here is how you can achieve your goal:

>>> d = {"fwt-master2": { "ipv4": { "rtr": { "ip": "1.2.3.4", "net": "3.4.5.6", "netlen": "24", "netmask": "255.255.255.0", "broadcast": "7.8.9.1" }}}}
>>> obj = []
>>> obj.append(d)
>>> obj
[{'fwt-master2': {'ipv4': {'rtr': {'net': '3.4.5.6', 'netlen': '24', 'ip': '1.2.3.4', 'netmask': '255.255.255.0', 'broadcast': '7.8.9.1'}}}}]
>>> key_list = ['netmask', 'broadcast', 'ip', 'net']
>>> def recursive_items(dictionary):
...     for key, value in dictionary.items():
...         if type(value) is dict:
...             yield from recursive_items(value)
...         else:
...             yield (key, value)
...
>>> def find_key(obj):
...     for e in obj:
...         for key, value in recursive_items(e):
...             if key in key_list:
...                 print(key, value)
...         for key, value in recursive_items(e):
...             if key in key_list and value == 0:
...                 print(double_quote(key),":", value)
...
>>> find_key(obj)
net 3.4.5.6
ip 1.2.3.4
netmask 255.255.255.0
broadcast 7.8.9.1

Have Fun

Ujjawal Khare
  • 756
  • 2
  • 7
  • 20