skip to Main Content

I’m trying to mass-delete a bunch of items from my spotify streaming data by specific artists (mostly just trying to remove a bunch of audiobooks). The data is in the form of a bunch of JSONs formatted like this:

  {
    "ts": "2017-05-04T13:26:03Z",
    "username": [redacted],
    "platform": "Android OS 5.1.1 API 22 (samsung, SM-J500H)",
    "ms_played": 11887,
    "conn_country": "GB",
    "ip_addr_decrypted": null,
    "user_agent_decrypted": null,
    "master_metadata_track_name": "Just Breathe",
    "master_metadata_album_artist_name": "Pearl Jam",
    "master_metadata_album_album_name": "Backspacer",
    "spotify_track_uri": "spotify:track:2m2dgHoAEZUkmxamsOwyQg",
    "episode_name": null,
    "episode_show_name": null,
    "spotify_episode_uri": null,
    "reason_start": "clickrow",
    "reason_end": null,
    "shuffle": "FALSE",
    "skipped": null,
    "offline": "FALSE",
    "offline_timestamp": 1.49385E+12,
    "incognito_mode": "FALSE"
  },
  {
    "ts": "2017-05-04T13:26:32Z",
    "username": [redacted],
    "platform": "Android OS 5.1.1 API 22 (samsung, SM-J500H)",
    "ms_played": 11887,
    "conn_country": "GB",
    "ip_addr_decrypted": null,
    "user_agent_decrypted": null,
    "master_metadata_track_name": "Just Breathe",
    "master_metadata_album_artist_name": "Pearl Jam",
    "master_metadata_album_album_name": "Backspacer",
    "spotify_track_uri": "spotify:track:2m2dgHoAEZUkmxamsOwyQg",
    "episode_name": null,
    "episode_show_name": null,
    "spotify_episode_uri": null,
    "reason_start": "clickrow",
    "reason_end": null,
    "shuffle": "FALSE",
    "skipped": null,
    "offline": "FALSE",
    "offline_timestamp": 1.49385E+12,
    "incognito_mode": "FALSE"
  },
  {
    "ts": "2017-05-04T13:30:17Z",
    "username": [redacted],
    "platform": "Android OS 5.1.1 API 22 (samsung, SM-J500H)",
    "ms_played": 0,
    "conn_country": "GB",
    "ip_addr_decrypted": null,
    "user_agent_decrypted": null,
    "master_metadata_track_name": "The Sirens of Time Part 1, Track 1",
    "master_metadata_album_artist_name": "Doctor Who",
    "master_metadata_album_album_name": "Main Range 1: The Sirens of Time",
    "spotify_track_uri": "spotify:track:64vQITW6SxB2fgxulsZ5Ix",
    "episode_name": null,
    "episode_show_name": null,
    "spotify_episode_uri": null,
    "reason_start": "clickrow",
    "reason_end": null,
    "shuffle": "FALSE",
    "skipped": null,
    "offline": "FALSE",
    "offline_timestamp": 1.4939E+12,
    "incognito_mode": "FALSE"
  },

How, for example, would I go about removing all elements where "master_metadata_album_artist_name" is equal to "Doctor Who"? And is there a way to do this for multiple JSON files at once?

Tried:

import json
obj  = json.load(open("Streaming_History_Audio_2016-2018_1.json"))
                                                      
for i in xrange(len(obj)):
    if obj[i]["master_metadata_album_artist_name"] == "Doctor Who":
        obj.pop(i)
        break
                                     
open("updated-file.json", "w").write(
    json.dumps(obj, sort_keys=True, indent=4, separators=(',', ': '))
)

Result:

C:Userstim_fDownloadsmy_spotify_data(2)Spotify Extended Streaming History 2023 - Copy>py del.py
Traceback (most recent call last):
  File "C:Userstim_fDownloadsmy_spotify_data(2)Spotify Extended Streaming History 2023 - Copydel.py", line 2, in <module>
    obj  = json.load(open("Streaming_History_Audio_2016-2018_1.json"))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:Userstim_fAppDataLocalProgramsPythonPython312Libjson__init__.py", line 293, in load
    return loads(fp.read(),
                 ^^^^^^^^^
  File "C:Userstim_fAppDataLocalProgramsPythonPython312Libencodingscp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 181341: character maps to <undefined>

2

Answers


  1. Chosen as BEST ANSWER

    update: Think I figured it out

    import json
    
    # Open the JSON file for reading
    with open('Streaming_History_Audio_2018_2.json', 'r', encoding="utf8") as json_file:
        data = json.load(json_file)
    
    # Filter out elements with "master_metadata_album_artist_name" as "Doctor Who"
    filtered_data = [item for item in data if item.get('master_metadata_album_artist_name') != "Doctor Who"]
    
    # Open the JSON file for writing
    with open('Streaming_History_Audio_2018_2_filtered.json', 'w') as json_file:
        json.dump(filtered_data, json_file, indent=2)
    
    print("Filtered data has been written to Streaming_History_Audio_2018_2_filtered.json")
    

  2. You are encountering an error while opening the file. This is most likely due to an encoding issue.
    Try this:

    obj = json.load(open("Streaming_History_Audio_2016-2018_1.json", encoding="utf8"))
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search