-3

I have a dictionary which contains the following json elements.

myjsonDictionary = \
{
  "Teams": {
    "TeamA": {
      "@oid": "123.0.0.1",
      "dataRequestList": {
        "state": {
          "@default": "0",
          "@oid": "2"
        }
      },
      "TeamSub": {
        "@oid": "3",
        "dataRequestList": {
          "state": {
            "@default": "0",
            "@oid": "2"
          }
        }
      }
    },

   # ....many nested layers
  }
}

I have the following issue and am currently very confused on how to solve this problem. I want to be able to parse this dictionary and get the concatenation of the "@oid" value and the respective "@oid" when I request the "key" such as "TeamA" or "TeamSub".

I have a function which takes in the gettheiDLevelConcatoid(myjsonDictionary, key).

I can call this function like this:

gettheiDLevelConcatoid(myjsonDictionary, key) where "key" is like "TeamA"

And the expected output should be "123.0.0.1.2". Note the 2 appended to the 123.0.0.1.

gettheiDLevelConcatoid(myjsonDictionary, key) where "key" is like TeamSub
Output is "123.0.0.1.3.2". Note the "3.2" added to the "123.0.0.1".

My current implementation:

def gettheiDLevelConcatoid(myjsonDictionary, key)
   for item in myjsonDictionary:
       if (item == key):
        #not sure what to do

I am so lost on how to implement a generic method or approach for this.

martineau
  • 119,623
  • 25
  • 170
  • 301
Laura Smith
  • 293
  • 3
  • 13

1 Answers1

1

With recursive traversal for specific keys:

def get_team_idlvel_oid_pair(d, search_key):
    for k, v in d.items():
        if k.startswith('Team'):
            if k == search_key:
                return '{}{}.{}'.format(d['@oid'] + '.' if '@oid' in d else '',
                                        v['@oid'], v['dataRequestList']['state']['@oid'])
            elif any(k.startswith('Team') for k_ in v):
                return get_team_idlvel_oid_pair(v, search_key)


print(get_team_idlvel_oid_pair(myjsonDictionary['Teams'], 'TeamA'))
print(get_team_idlvel_oid_pair(myjsonDictionary['Teams'], 'TeamSub'))

Sample output:

123.0.0.1.2
123.0.0.1.3.2
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • Is it possible to make ```if k.startswith('Team'):``` more generic as it could be some other string other than "Team". – Laura Smith Jul 30 '19 at 19:50
  • @LauraSmith, if it's not related to conceptual key names, than you would have to check for `'@oid'`, `'dataRequestList']['state']` keys in every dictionary, which is more bothering – RomanPerekhrest Jul 30 '19 at 20:07
  • Not sure if you get what I mean. In the call to the get_team_idlvel_oid_pair, can I call is by ```get_team_idlvel_oid_pair(myjsonDictionary, "TeamA") rather than the one you suggested and then iterate over the dictionary and just look for the "TeamA" and return the concatenated "@oid" values as stated in the question above. – Laura Smith Jul 30 '19 at 20:10
  • I tried to modify your code per my approach as stated in the previous comment but I am unable to access the nested nodes and only the outer level nodes – Laura Smith Jul 30 '19 at 20:30
  • Please do let me know if you have updates for me. Any – Laura Smith Jul 31 '19 at 01:02