0

I have a list of lists with several lines like this (the example below shows line 4 out of 178):

result[4]

['activity',
 'root',
 'children',
 0,
 'children',
 0,
 'children',
 0,
 'children',
 0,
 'children',
 0,
 'children',
 0,
 'visibility']

I would like to convert the structure above into something like this:

['activity']['root']['children'][0]['children'][0]['children'][0]['children'][0]['children'][0]['children'][0]['visibility']

So that it can be used in a loop to access nodes in a heavily nested JSON file.

What I envisage is something like this:

def convert_to_path(list):
     do stuff

     return valid_path


def use_converted_path(json_data, converted_path):
            do stuff

            return
            The function would then return the same as if I did json_data['activity']['root']['children'][0]['children'][0]['children'][0]['children'][0]['children'][0]['children'][0]['visibility']

original_list = result[4]
valid_path = convert_to_path(original_list)
use_converted_path(valid_path) (output the JSON value )

I see it as a two functions job but I'm open to any suggestions!

RBH
  • 40
  • 1
  • 6
  • The answer I duped this to works just as well for a mix of lists and dictionaries. – Martijn Pieters Jul 29 '18 at 21:05
  • Thanks, I have been searching for it but I guess I was using the wrong terminology. I think leaving the question might help future newbies like me with the same problem! :D – RBH Jul 29 '18 at 21:19

0 Answers0