Python如何在嵌套列表中插入对象

Python如何在嵌套列表中插入对象,python,json,Python,Json,如何在嵌套列表中插入对象。我得到的json响应不会针对特定条件返回特定对象 data.json 注意:在“步骤”列表中,第三个索引没有返回结果 我想要实现的目标: with open('data.json') as data_file: data = json.load(data_file) #nested for loops?? #if x.has_key('result') == False #insert result object 如果缺少“result”对象,我想插

如何在嵌套列表中插入对象。我得到的json响应不会针对特定条件返回特定对象

data.json

注意:在“步骤”列表中,第三个索引没有返回结果

我想要实现的目标:

with open('data.json') as data_file:  
     data = json.load(data_file) 

#nested for loops??
#if x.has_key('result') == False
#insert result object
如果缺少“result”对象,我想插入它

到目前为止我所做的(伪):

with open('data.json') as data_file:  
     data = json.load(data_file) 

#nested for loops??
#if x.has_key('result') == False
#insert result object

以下应该是你的名字:

for step in data[0]['elements'][0]['steps']:
    if 'result' not in step:
        step['result'] = {"status": "skipped"}
更通用的解决方案:

for d in data:
    for element in d['elements']:
        for step in element['steps']:
            if 'result' not in step:
                step['result'] = {"status": "skipped"}

以下应该是你的名字:

for step in data[0]['elements'][0]['steps']:
    if 'result' not in step:
        step['result'] = {"status": "skipped"}
更通用的解决方案:

for d in data:
    for element in d['elements']:
        for step in element['steps']:
            if 'result' not in step:
                step['result'] = {"status": "skipped"}