Note: I have previously asked a similar question but couldn't get any useful response.
I have to be able to parse the CountriesDictionary and for any given key such as "London", "Norway", "Sweden", and other countries, I want to be able to he concatenation the "@myID" value with the respective "@myID" in the status node when I request the "key" such as "London" or "Norway".
For example #1: Currently my method can only do this: If I call my method,
getconcatenatedstring(CountriesDictionary , "London"), I should get the result as"35C.20C".To get the result, I have to concatenate the
@myIDof London which is35Cwith the @myIDin the status node which is20C.For example #2: My method cannot do this. Note: Norway is a nested node in London!
If I call my method,
getconcatenatedstring(CountriesDictionary , "Norway"), I should get the result as"35C.12Q.20D".To get the result, I have to concatenate the
@myIDof London which is35Cwith the@myIDin the Norway node which is12Qand finally the@myIDof the status node inInfoListwhich is20D.Example #3: My current method can get this output
getconcatenatedstring(CountriesDictionary , "Sweden"), the output I should get is"32C.15D".
To get the result, I need to concatenate the @myID of Sweden which is 32C with the @myID in the status node of Sweden which is 15D.
I have a json dictionary with the following information.
CountriesDictionary = {
"Countries": {
"London": {
"@loc": "porch",
"@myID": "35C",
"Tid": "1",
"InfoList": {
"status": {
"@default": "0",
"@myID": "20C"
},
"count": {
"@default": "0",
"@myID": "1"
}
},
"Norway": {
"@loc": "backyard",
"@myID": "12Q",
"Tid": "2",
"InfoList": {
"status": {
"@default": "0",
"@myID": "20D"
},
"count": {
"@default": "0",
"@myID": "2"
}
}
}
},
"Sweden": {
"@loc": "backyard",
"@myID": "32C",
"Tid": "2",
"InfoList": {
"status": {
"@default": "0",
"@myID": "15D"
},
"count": {
"@default": "0",
"@myID": "2"
}
}
},
// many more other countries with similar structure as above
}
}
My method implementation. I am not sure what is missing in this method as I am unable to get the desired result for example #2 as described above.
def getconcatenatedstring(d, search_key):
for k1, v1 in d.items():
for k,v in v1.items():
if k == search_key:
print (k)
return '{}{}.{}'.format(d['@myID'] + '.' if '@myID' in d else '',
v['@myID'], v['InfoList']['status']['@myID'])
# Example call to method
getconcatenatedstring(CountriesDictionary, "Sweden")