Search code examples
pythondictionaryspotifyspotipy

How do I iterate through a nested list from Spotipy's playlist_items function and only print out the fields I'm interested in?


I am attempting to get all the track names and corresponding artist from a playlist using Spotipy. I am at the point where I am able to get a json dump of the fields, but I cannot figure out how to iterate through the dump and print out the fields I'm interested in.

Here is what I'm using to get all information about the playlist:

playlist = spotipy.playlist_items(playlist_id)

If I then print(json.dumps(playlist, indent=2)), then I get the following output:

{
  "collaborative": false,
  "description": "",
  "external_urls": {
    "spotify": "https://open.spotify.com/playlist/50uWPcNFdJElMVZWo0IebB"
  },
  "followers": {
    "href": null,
    "total": 0
  },
  "href": "https://api.spotify.com/v1/playlists/50uWPcNFdJElMVZWo0IebB?additional_types=track",
  "id": "50uWPcNFdJElMVZWo0IebB",
  "images": [
    {
      "height": 640,
      "url": "https://i.scdn.co/image/ab67616d0000b273942a0c9ac8f1def7c8805044",
      "width": 640
    }
  ],
  "name": "Spotipy Test",
  "owner": {
    "display_name": "username",
    "external_urls": {
      "spotify": "https://open.spotify.com/user/username"
    },
    "href": "https://api.spotify.com/v1/users/username",
    "id": "username",
    "type": "user",
    "uri": "spotify:user:username"
  },
  "primary_color": null,
  "public": false,
  "snapshot_id": "Nyw0OTk4MDg1NDM3NzRlOWI2MGY2MDc2ZjMxNTE4OGFkZWQ5Y2QyZDdk",
  "tracks": {
    "href": "https://api.spotify.com/v1/playlists/50uWPcNFdJElMVZWo0IebB/tracks?offset=0&limit=100&additional_types=track",
    "items": [
      {
        "added_at": "2021-11-17T07:32:59Z",
        "added_by": {
          "external_urls": {
            "spotify": "https://open.spotify.com/user/username"
          },
          "href": "https://api.spotify.com/v1/users/username",
          "id": "username",
          "type": "user",
          "uri": "spotify:user:username"
        },
        "is_local": false,
        "primary_color": null,
        "track": {
          "album": {
            "album_type": "album",
            "artists": [
              {
                "external_urls": {
                  "spotify": "https://open.spotify.com/artist/6fOMl44jA4Sp5b9PpYCkzz"
                },
                "href": "https://api.spotify.com/v1/artists/6fOMl44jA4Sp5b9PpYCkzz",
                "id": "6fOMl44jA4Sp5b9PpYCkzz",
                "name": "NF",
                "type": "artist",
                "uri": "spotify:artist:6fOMl44jA4Sp5b9PpYCkzz"
              }
            ],
            "available_markets": [],
            "external_urls": {
              "spotify": "https://open.spotify.com/album/46xdC4Qcvscfs3Ai2RIHcv"
            },
            "href": "https://api.spotify.com/v1/albums/46xdC4Qcvscfs3Ai2RIHcv",
            "id": "46xdC4Qcvscfs3Ai2RIHcv",
            "images": [
              {
                "height": 640,
                "url": "https://i.scdn.co/image/ab67616d0000b273942a0c9ac8f1def7c8805044",
                "width": 640
              },
              {
                "height": 300,
                "url": "https://i.scdn.co/image/ab67616d00001e02942a0c9ac8f1def7c8805044",
                "width": 300
              },
              {
                "height": 64,
                "url": "https://i.scdn.co/image/ab67616d00004851942a0c9ac8f1def7c8805044",
                "width": 64
              }
            ],
            "name": "The Search",
            "release_date": "2019-07-26",
            "release_date_precision": "day",
            "total_tracks": 20,
            "type": "album",
            "uri": "spotify:album:46xdC4Qcvscfs3Ai2RIHcv"
          },
          "artists": [
            {
              "external_urls": {
                "spotify": "https://open.spotify.com/artist/6fOMl44jA4Sp5b9PpYCkzz"
              },
              "href": "https://api.spotify.com/v1/artists/6fOMl44jA4Sp5b9PpYCkzz",
              "id": "6fOMl44jA4Sp5b9PpYCkzz",
              "name": "NF",
              "type": "artist",
              "uri": "spotify:artist:6fOMl44jA4Sp5b9PpYCkzz"
            }
          ],
          "available_markets": [],
          "disc_number": 1,
          "duration_ms": 248053,
          "episode": false,
          "explicit": false,
          "external_ids": {
            "isrc": "USUM71907048"
          },
          "external_urls": {
            "spotify": "https://open.spotify.com/track/3oLe5ZILASG8vU5dxIMfLY"
          },
          "href": "https://api.spotify.com/v1/tracks/3oLe5ZILASG8vU5dxIMfLY",
          "id": "3oLe5ZILASG8vU5dxIMfLY",
          "is_local": false,
          "name": "The Search",
          "popularity": 74,
          "preview_url": "https://p.scdn.co/mp3-preview/c26b3dedc967f5009451a693ac275e50c6ecb53c?cid=1f51bd6bdbd94e34884f3ec17c0f68ed",
          "track": true,
          "track_number": 1,
          "type": "track",
          "uri": "spotify:track:3oLe5ZILASG8vU5dxIMfLY"
        },
        "video_thumbnail": {
          "url": null
        }
      },
      {
        "added_at": "2021-11-17T07:33:24Z",
        "added_by": {
          "external_urls": {
            "spotify": "https://open.spotify.com/user/username"
          },
          "href": "https://api.spotify.com/v1/users/username",
          "id": "username",
          "type": "user",
          "uri": "spotify:user:username"
        },
        "is_local": false,
        "primary_color": null,
        "track": {
          "album": {
            "album_type": "album",
            "artists": [
              {
                "external_urls": {
                  "spotify": "https://open.spotify.com/artist/3uL4UpqShC4p2x1dJutoRW"
                },
                "href": "https://api.spotify.com/v1/artists/3uL4UpqShC4p2x1dJutoRW",
                "id": "3uL4UpqShC4p2x1dJutoRW",
                "name": "Ghostemane",
                "type": "artist",
                "uri": "spotify:artist:3uL4UpqShC4p2x1dJutoRW"
              }
            ],
            "available_markets": [],
            "external_urls": {
              "spotify": "https://open.spotify.com/album/4pzCKSWYNM2yUWrNvycC6e"
            },
            "href": "https://api.spotify.com/v1/albums/4pzCKSWYNM2yUWrNvycC6e",
            "id": "4pzCKSWYNM2yUWrNvycC6e",
            "images": [
              {
                "height": 640,
                "url": "https://i.scdn.co/image/ab67616d0000b27347fa4895a04fbb517fd42070",
                "width": 640
              },
              {
                "height": 300,
                "url": "https://i.scdn.co/image/ab67616d00001e0247fa4895a04fbb517fd42070",
                "width": 300
              },
              {
                "height": 64,
                "url": "https://i.scdn.co/image/ab67616d0000485147fa4895a04fbb517fd42070",
                "width": 64
              }
            ],
            "name": "Hexada",
            "release_date": "2017-09-05",
            "release_date_precision": "day",
            "total_tracks": 10,
            "type": "album",
            "uri": "spotify:album:4pzCKSWYNM2yUWrNvycC6e"
          },
          "artists": [
            {
              "external_urls": {
                "spotify": "https://open.spotify.com/artist/3uL4UpqShC4p2x1dJutoRW"
              },
              "href": "https://api.spotify.com/v1/artists/3uL4UpqShC4p2x1dJutoRW",
              "id": "3uL4UpqShC4p2x1dJutoRW",
              "name": "Ghostemane",
              "type": "artist",
              "uri": "spotify:artist:3uL4UpqShC4p2x1dJutoRW"
            }
          ],
          "available_markets": [],
          "disc_number": 1,
          "duration_ms": 124538,
          "episode": false,
          "explicit": true,
          "external_ids": {
            "isrc": "TCADG1741539"
          },
          "external_urls": {
            "spotify": "https://open.spotify.com/track/2Vc6NJ9PW9gD9q343XFRKx"
          },
          "href": "https://api.spotify.com/v1/tracks/2Vc6NJ9PW9gD9q343XFRKx",
          "id": "2Vc6NJ9PW9gD9q343XFRKx",
          "is_local": false,
          "name": "Mercury: Retrograde",
          "popularity": 76,
          "preview_url": "https://p.scdn.co/mp3-preview/7d4cb282d38d48d85e9faedb3ea5e74546281d07?cid=1f51bd6bdbd94e34884f3ec17c0f68ed",
          "track": true,
          "track_number": 4,
          "type": "track",
          "uri": "spotify:track:2Vc6NJ9PW9gD9q343XFRKx"
        },
        "video_thumbnail": {
          "url": null
        }
      }
    ],
    "limit": 100,
    "next": null,
    "offset": 0,
    "previous": null,
    "total": 2
  },
  "type": "playlist",
  "uri": "spotify:playlist:50uWPcNFdJElMVZWo0IebB"
}

And this is only for two songs being in the playlist!

At this point my limited knowledge of python fails me, because I dont know how to proceed from here. All I am looking for is the track name and the corresponding artist.

So, how do I go through the massive nested list, and only print out the fields Im interested in?


Solution

  • You can use the fields variable to specify the information you want. For example with playlist = sp.playlist_items(paylist_id, fields='items.track.artists.name,items.track.name'), you'll get only the artist name(s) and track name information back: {"items": [{"track": {"artists": [{"name": "Avicii"}], "name": "Levels - Radio Edit"}}, {"track": {"artists": [{"name": "Avicii"}], "name": "Wake Me Up - Radio Edit"}}]}