Python Forum

Full Version: finding and deleting json object
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi
I have this JSON-
{
    "ticker_tape_1": [
        {
            "id": 1,
            "Title": "Message 1",
            "txt": "This is message 1",
            "start_date": "01/04/2020",
            "start_time": "16:20",
            "duration": "1"
        },
        {
            "id": 2,
            "Title": "Message 2",
            "txt": "This is message 2",
            "start_date": "01/04/2020",
            "start_time": "16:20",
            "duration": "1"
        }
    ]
}
I need to find and delete from ticker_tape_1 the element with an id of 1
How is the most efficient way of doing this?
You can use the JSON module to read that into a python object.

At that point, the easiest thing might be to just copy everything in ticker_tape_1 to a new list, but leave out the elements you don't want.

tt1 = mydata["ticker_tape_1"]
mydata["ticker_tape_1"] = [x for x in tt1 if x.get("id") != 1]