Skip to content
Advertisement

How do i remove a specific json value in python?

I’m making a playlist website that can create display and delete playlists and songs. I’m using a JSON file to store the data. The only feature that isn’t working is the delete song feature.

This is my JSON file

"Playlists": [
    {
        "ID": 1,
        "Name": "Car Boosted",
        "Songs": [
            {
                "Name": "Dusk Till Dawn",
                "Artist": "Sia",
                "Link": "https://www.youtube.com/watch?v=6ADdqsvlqtU"
            },
            {
                "Name": "Blinding Lights",
                "Artist": "The Weekend",
                "Link": "https://www.youtube.com/watch?v=4NRXx6U8ABQ"
            }
        ],
        "Date": "16 / 03 / 2022"
    },
    {
        "ID": 2,
        "Name": "Workout Playlists",
        "Songs": [
            {
                "Name": "Dusk Till Dawn",
                "Artist": "Sia",
                "Link": "https://www.youtube.com/watch?v=6ADdqsvlqtU"
            },
            {
                "Name": "Blinding Lights",
                "Artist": "The Weekend",
                "Link": "https://www.youtube.com/watch?v=4NRXx6U8ABQ"
            },
            {
                "Name": "Till I Collapes",
                "Artist": "Eminem",
                "Link": "https://www.youtube.com/watch?v=Obim8BYGnOE"
            },
            {
                "Name": "Lose Yourself",
                "Artist": "Eminem",
                "Link": "https://www.youtube.com/watch?v=_Yhyp-_hX2s"
            }
        ],
        "Date": "25 / 04 / 2022"
    }
]

I’m using the ID of the playlist and the index of the song to locate it in the list and using that to also delete it. This is my “deleteSong” function:

def deleteSongFromJson(songID, pid):
"""deletes Songs Form Playlist

Args:
        songID (int): ID Of Song
        id (itn): ID Of Playlist
"""
pid = int(pid)
songID = int(songID)
with open('databse.json','r+') as file:
    data = json.load(file)
    playlists = data['Playlists']
    for playlist in playlists:
     if playlist['ID'] == id:
         songs = playlist["Songs"]
         del songs[songID]
         file.seek(0)
         json.dump(data, file, indent = 4)

         

When I run the function it deletes the song from the right playlist but pastes the last bit of the list and gets pasted to the bottom of the JSON file.

E.g.

                "Name": "Blinding Lights",
                "Artist": "The Weekend",
                "Link": "https://www.youtube.com/watch?v=4NRXx6U8ABQ"
            }
        ],
        "Date": "17 / 05 / 2022"
    }
]
    "Artist": "The Weekend",
                "Link": "https://www.youtube.com/watch?v=4NRXx6U8ABQ"
            }
        ],
        "Date": "17 / 05 / 2022"
    }
]

Advertisement

Answer

I think your problem is with trying to read and write the file in one go, which can cause weird cursor issues.

Try refactoring your code to have two separate open statements like the following (untested):

def deleteSongFromJson(songID, pid):
    """deletes Songs from Playlist

    Args:
        songID (int): ID Of Song
        id (itn): ID Of Playlist
    """
    pid = int(pid)
    songID = int(songID)

    with open('databse.json','r') as file:
        data = json.load(file)

    for playlist in data['Playlists']:
        if playlist['ID'] == id:
            del playlist["Songs"][songID]

    with open('databse.json','w') as file:
        json.dump(data, file, indent=4)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement