如何将Python列表转换为有效的JSON路径?

时间:2018-07-29 20:55:32

标签: python json path nodes depth

我有一个包含几行的列表列表(以下示例显示了178行中的第4行):

result[4]

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

我想将上面的结构转换为如下形式:

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

以便可以在循环中使用它来访问高度嵌套的JSON文件中的节点。

我设想的是这样的:

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 )

我将其视为一项具有两个职能的工作,但我愿意接受任何建议!

0 个答案:

没有答案