Remove python dict item from nested json file

后端 未结 1 918
粉色の甜心
粉色の甜心 2021-01-07 06:51

I have a JSON file that I fetch from an API that returns KeyError:0 while I attempt to remove items in a python dict. I assume its a combination of my lack of skill and form

相关标签:
1条回答
  • 2021-01-07 07:22

    This is incorrect:

    # remove items
    for i in xrange(len(obj)):
        if obj[i]["ip_address_1"] == "192.168.1.1":
            obj.pop(i)
    

    You are iterating over an object as if it were a list.

    What you want to do:

    for sub_obj in obj["response"]["alerts"]:
        if sub_obj["ip_address_1"] == "192.168.1.1":
            sub_obj.pop("ip_address_1")
    
    0 讨论(0)
提交回复
热议问题