0

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"
    }
]
1
  • first only read all data and close file, next remove values from dictionary, and later open only for writing and write all data back. Opening for writing will remove old data from file. When you read and write at the same time then it doesn't remove old data but overwrite existing text and doesn't crop it. Commented May 17, 2022 at 20:59

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.