0

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".

  1. 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 @myID of London which is 35C with the @myID in the status node which is 20C.

  2. 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 @myID of London which is 35C with the @myID in the Norway node which is 12Q and finally the @myID of the status node in InfoList which is 20D.

  3. 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") 
Laura Smith
  • 293
  • 3
  • 13
  • just another version of the mentioned duplicate question (posted by OP previously) – RomanPerekhrest Aug 01 '19 at 13:30
  • @RomanPerekhrest I have provided the method of implemetation. I am unsure why this function doesnt traveres it. :( – Laura Smith Aug 01 '19 at 13:32
  • @RomanPerekhrest This question is not fully a duplicate as the previously asked question doesnt answer my question. I am unable to solve the bug and I am not so experienced in python unlike you who knows it better. Please do help me to resolve the issue. Thanks – Laura Smith Aug 01 '19 at 13:54

0 Answers0