decodebytes
decodebytes

Reputation: 421

Parsing nested key from dict (generated from json)

I am looking at anisble python API, which returns values via json, which can then be stored as dict

{
"dark" : {
   "web1.example.com" : "failure message"
},
"contacted" : {
   "web2.example.com" : 1
}
}

I finding it a challenge to dict.get the nested value, of web2.example.com, under contacted.

Thanks in advance. I would give my examples, but they are failing to provide anything usable.

Reference: https://github.com/ansible/ansible/blob/12a2585e84be7fdf40e07ca1415f4938e95de1f3/docsite/rst/developing_api.rst

Upvotes: 1

Views: 659

Answers (1)

Mangu Singh Rajpurohit
Mangu Singh Rajpurohit

Reputation: 11420

Suppose you get dictionary as follow :-

dictFromJSON = {
"dark" : {
   "web1.example.com" : "failure message"
},
"contacted" : {
   "web2.example.com" : 1
}
}

Then you can access values as follow :--

for strKey, dictVal in dictFromJSON.items():
    if strKey == "contacted":
       print dictVal.keys()

Upvotes: 1

Related Questions