How to map different API responses in woklow action

Hi, i have an API that delivers different results. Before i make the call i dont know what i get as a result.

In another post i found the tip to manually enter the response and put everything in there that might be a response.
So far so good. Now i ran into 2 issues:

  1. The result of the API Call does not seem to be a list anymore in the workflow (i get an error when trying to perform this result as a list.
    (Step 1 is a list if i don’t map it manually)

  2. How do i then tell bubble what to set as a “key” value? I guess to use a “or” expression but that does not exists so what can i do?

If you entered everything you wanted (every possible options) manually as JSON in the api connector and need Bubble to understand the results will always be an array/list, you’ll need to add “[” at the beginning of the json response and “]” at the end everytime you edit it… The “manual” json response editor doesn’t remember that its an array, so you’ve got to put everything in [], everytime. :wink:

1 Like

Should i put every option in those brackets or everything in one?
Here is the screenshot of my “manual response” Thank you so much for you help @Future

@Future
do you know how i can solve the other issue?

For example i want to set a data field “value” and it should be either “energyImportTotal” or “energy” or “external temperature”

→ its either one of these are getting send back from the api. (or some more in the future :smiley: )

Actually, you just have to add “[” before the initial “{” and “]” after the final “}”. (everytime you’re going to edit it… because bubble doesn’t remember)

Not too sure about second question but for example… If sometimes you have a result like “energyImportTotal”: 10, and sometimes it’s not there but you have “energy”: “value”… You just make sure to include every possible options in the response editor… In your workflows, Bubble will use them if they’re in the response… and won’t do anything if its not there…

That is the perfct case for an option set. If you create an option set called APIResponse and then add the three values mentioned above, you can remap the value types on the api return and make the one field of type APIResponse. Then it will always be one of those.

How do i implement this properly? Any tips @williamtisdale ?

After reading this post again, my initial statement was incorrect for your use case but here is a full solution for both problems. If you say, Make a change to Thing -> field "value" = "Result of Step 1's energyImportTotal is not empty: formatted as text then in the format for text you will do if yes then Result of Step 1's energyImportTotal and for no you will add Result of Step 1's energy is not empty: formatted as text then in the format for text you will do if yes then Result of Step 1's energy and for no you will add Result of Step 1's externalTemperature is not empty: formatted as text then in the format for text you will do if yes then Result of Step 1's externalTemperature and for no you will leave blank or put a 0 if that is your last field or you want a default value. At the end you will have to say converted to number.

As for the lists anything that needs to be a list just needs to be placed inside of the array brackets ([]). So for example:
You have this:
{"energy": 0}
Change it to this:
{"energy": [0]}
Now energy will be a list of numbers.

1 Like

This does not seem to work. I need to do that mapping in the API Call itself (/the workflow for the API Call) And there it waits for a number so i cant use the “formatted as a text” operator :frowning:
→ At least this is what bubble tells me.

Any other ideas?
@williamtisdale

Remember at the end add “converted to number”