1
{
    "Food" : {
        "Abc" : "xyz",
        "Meat" : {
            "kind" : "Turkey",
            "Size" : {},
            "Id" : "9747"
        },
        "Fruit" : [{
                "Name" : "Orange",
                "Code" : "Fr",
                "NewCode" : "Fu1"
            }, {
                "Name" : "Apple",
                "Code" : "App",
                "Id" : "23"
            }, {
                "Name" : "Grape",
                "Code" : "Grp"
            }
        ],
        "Vegetables" : {}
    }
}

I need check if 'Id' is present in Meat, Fruit and Vegetables and if it is present then delete the Id(Key - Value) pair. Sometimes Id is present (ex. Meat and Fruit) or not present for a particular section(Fruit --> Grape) or sometime nothing is present (Vegetables). I've been able to remove Abc(Key-Value) pair but i've not been able to do the same for Meat, Fruit and Vegetables dictionaries. Can someone help me out.

5
  • Are you trying to delete only the ID key-value pairs, or it's parent? Commented Apr 13, 2018 at 16:31
  • I only need to delete Id Key-value pairs Commented Apr 13, 2018 at 18:08
  • Since you have a nested structure, you are going to have to use recursion to visit all sub structures. It is complicated by being arbitrarily deep and a mix of lists and dicts. Commented Apr 14, 2018 at 17:02
  • Please reopen this. The method of deleting keys inside a nested dict that includes lists as values is not covered by the marked duplicate. It is not really a duplicate at all. Commented Apr 15, 2018 at 18:10
  • How can i reopen the issue? Commented Apr 16, 2018 at 0:59

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.