0

I have had trouble appending id's to a separate list as I parse through the JSON I receive from Spotify's "Users Saved Tracks" endpoint.

The JSON received looks like this:

    {
    "href": "https://api.spotify.com/v1/me/tracks?offset=0&limit=20",
    "items": [
        {
            "added_at": "2021-11-16T13:56:51Z",
            "track": {
                "album": {
                    "album_type": "single",
                    "artists": [
                        {
                            "external_urls": {
                                "spotify": "https://open.spotify.com/artist/3iKDeO8yaOiWz7vkeljunk"
                            },
                            "href": "https://api.spotify.com/v1/artists/3iKDeO8yaOiWz7vkeljunk",
                            "id": "3iKDeO8yaOiWz7vkeljunk",
                            "name": "Heavenward",
                            "type": "artist",
                            "uri": "spotify:artist:3iKDeO8yaOiWz7vkeljunk"
                        }
                    ],
                    "available_markets": [
                       
                ],
                "disc_number": 1,
                "duration_ms": 224838,
                "explicit": false,
                "external_ids": {
                    "isrc": "QZK6P2040977"
                },
                "external_urls": {
                    "spotify": "https://open.spotify.com/track/6mJ1nbmQOm6iNClo71K5O6"
                },
                "href": "https://api.spotify.com/v1/tracks/6mJ1nbmQOm6iNClo71K5O6",
                "id": "6mJ1nbmQOm6iNClo71K5O6",
                "is_local": false,
                "name": "Hole",
                "popularity": 33,
                "preview_url": "https://p.scdn.co/mp3-preview/c425dc91bdb19f1cddf2b35df08e30a03290c3c0?cid=8c9ee97b95854163a250399fda32d350",
                "track_number": 1,
                "type": "track",
                "uri": "spotify:track:6mJ1nbmQOm6iNClo71K5O6"
            }
        }

Right now my code that I am using to parse looks like this:

def getLikedTrackIds(session):

    url = 'https://api.spotify.com/v1/me/tracks'
    payload = makeGetRequest(session, url)

    if payload == None:
        return None
    
    liked_tracks_ids = []
    for track in payload['items']:
        for attribute in track['track']:
            if (attribute == 'id'):
                app.logger.info(f"\n\nTrack ID: {attribute}")
                liked_tracks_ids.append(attribute)
    
    return liked_tracks_ids

My liked_track_ids is filled with the string "id", for each song:

[ "id", "id", "id", "id"....]

Can anyone provide insight as to what I am doing wrong?

3
  • As a side note, the json that i posted is only a short snippet of what I actually received. There are about 20 other "track" json objects under the one i have in the question. Commented Nov 18, 2021 at 3:03
  • 1
    Your code literally has if (attribute == 'id'): followed by liked_tracks_ids.append(attribute) - so that's your answer right there. You probably wanted liked_tracks_ids.append(track[attribute]) or just liked_tracks_ids.append(track['id']) Commented Nov 18, 2021 at 3:26
  • @Grismar is correct, although the solution would be liked_tracks_ids.append(track['track']['id']) Commented Nov 18, 2021 at 8:15

1 Answer 1

1

Already commented under the question but your code can be simplified by getting rid of the loop:

def getLikedTrackIds(session):

    url = 'https://api.spotify.com/v1/me/tracks'
    payload = makeGetRequest(session, url)

    if payload == None:
        return None
    
    liked_tracks_ids = []
    for track in payload['items']:
        liked_id =  track['track'].get('id', None)
        if liked_id:
            app.logger.info(f"\n\nTrack ID: {liked_id}")
            liked_tracks_ids.append(liked_id)
    
    return liked_tracks_ids
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.