Converting API Connector JSON into Bubble lists (when items have variable lengths)

Hi folks,

I have a process for turning data obtained through the API connector into Bubble objects (using a recursive backend workflow). It works great when the JSON data has a consistent format, but I’ve run into an issue when things aren’t uniform.

The data is coming from Spotify, and a simplified view of the JSON looks like the example below. As you can see each “Track” has one “Name” but can have one or more “Artists” (when there are collaborators on a song).

{
“Tracks”: {
“Items”: [
{
“Track”: {
“Name”: “Track Name”,
“Artists”: [
{“Name”: “Artist A”},
{“Name”: “Artist B”}
]
}
},
{
“Track”: {
“Name”: “Track Name”,
“Artists”: [
{
“Name”: “Artist C”
}
]
}
},
{
“Track”: {
“Name”: “Track Name”,
“Artists”: [
{
“Name”: “Artist D”
},
{
“Name”: “Artist E”
},
{
“Name”: “Artist F”
}
]
}
},
{
“Track”: {
“Name”: “Track Name”,
“Artists”: [
{
“Name”: “Artist G”
}
]}}]}}

What I want to extract from this JSON is only the first artist name for each track, so a correct outcome would be an artist list like this:

Artists [“Artist A”, “Artist C”, “Artist D”, “Artist G”]

But what I get is the whole list:

Artists [“Artist A”, “Artist B”, “Artist C”, “Artist D”, “Artist E”, “Artist F”, “Artist G”]

The way I’m doing it in Bubble is like the screenshot below. Basically, I call the backend workflow passing in a list of artists, a lists of track names, an index and the track count. Then on the backend it cycles through the data creating Bubble objects until it reaches the end of the list.

The problem is when there are more than one artist, as it’s pulling all of them which means the Bubble objects get created with artists that are out of sync with the track names.

You can see in the Artists field it’s taking the result of the Spotify API call, and getting:

Each item’s track artist’s name.

What I really need is a way for the list to only have the first item from each artist, but I haven’t been able to figure it out.

If I try putting :first item in there to create:

Each item’s track artist:first item’s name

It just takes the first item and is no longer a list. I need a way to insert the :first item inside the expression vs. at the end, but that’s not possible in the expression builder.

For example, in the screenshot below, I can get the list of artists for the track, and I’d want to then choose :first item, but have it be the :first item for every artist. As soon as I introduce :first item it ends the ability of it to be a list.

The last screenshot shows what happens if I insert :first item (leaving only the name as an option vs each item’s name).

I’ve tried to use a filter and a ton of other approaches but can’t for the life of me get it to work.

I can’t make the app public (wish I could make the editor view public for just one page).

Doesn’t anyone have an idea how I can use the expression builder to pull this first item of each?

Thanks in advance,

Oli.

You are probably not using stuff correctly.
What I understand is that you want to create a DB item for each item in the playlist from spotify. Right?

Question Why to not use schedule on a list instead of recursive workflow?

For recursive WF, in most case I prefer to use a DB item with a field of the API type and use this one to get data for each item in the list (so you can also fetch nested list)

2 Likes

Thanks, Jici.
When I set that up to schedule on a list (using Get Playlist tracks item which is what the API returns), I don’t know how to define the data coming in to the backend.

I can set up the call, but on the backend, if I’m trying to do “detect request data” I don’t know how to trigger that endpoint with some actual data.

When I set it to “detect request data” and go back to where I’m calling it I see another field that I can’t get to resolve and it’s saying the API workflow is no longer an option.

Can you help point me in the right direction for getting the request data working?

Thanks so much.

Actually, I think I figured it out using the manual definition and stepping through to find the right items.

Thanks so much for pointing me in the right direction! You’re a life saver.

1 Like

I’m having some issues with this approach actually @Jici - specifically that it doesn’t seem to like processing the list in the correct order. The order is an essential component of this because it’s base don a live Spotify playlist, but in testing, it is incredibly inconsistent in the order of the songs it processes when doing the list approach.

To try and get more granular I also add the current date/time as a unix ms timestamp to the DB object to be as accurate as possible (then I’m rerunning the list separately to set up the index values), but both the created date and the timestamps are very erratic in terms of the correct order.

I wish processing a list provided an index of where it is in the list.

Curious if you’ve ever dealt with this type of issue (ensuring the correct order).

Any advice much appreciated.

There’s no playlist order number in the payload? Actually, the index item is not available for Scheduled on a list (have been requested a lot of time, so add your voice to this request :wink: )
Try to add a higher interval (2 seconds)
If this is not enough, you may need to go back to recursive WF. Use DB solution I’ve suggested

2 Likes

Yeah sadly there is no index value in the Spotify payload. I think I’ve found another way around it. Basically, I store a list of the track URIs from the payload (this is correctly ordered), then I run through them afterwards setting the index based on the position of the matched URIs). Not exactly smooth but it’s accurate.

1 Like