JSON object dynamic values not working

I am trying to add a dynamic value in an API call but I keep getting the following error.

There was an issue setting up your call.

Raw response for the API
Status code 400
{“code”:“VALIDATION_ERROR”,“description”:“The payload is invalid.”,“details”:[{“code”:“WRONG_TYPE”,“description”:“should be object”,“field”:“/fields/0/properties/choices/0”,“in”:“body”}]}

The output of my dynamic value is made with javascript and has the following code and output.

const skill_list = [Search for skill loops:each item’s skill_js];
result = “”;
skill_list.forEach(function(skill_name) {
result = {label: skill_name}
});
let value = JSON.stringify(result);
console.log(value);
bubble_fn_choices(value);

and the output " bubble_fn_choices(value);" is the following; {“label”:“SQL (Programming Language)”}

But this does not work even though when I type it manually it does work as you can see below.


Why do you use js to create your choices value? You can do that in Bubble using format as text on the list of choices.
Can you sahre the ouput of your JS? (Console.log value)

Hello,

This is the code:

const skill_list = [Search for skill loops:each item's skill_js];
result = "";
skill_list.forEach(function(skill_name) {
result += '{"label": "' + skill_name + '"}, '
});
result = result.slice(0, -2);
console.log(result);
bubble_fn_choices(result);

This is the output we get from the console.log(result):

And this is the what Bubble gets with passing the argument in the last function:
value

Which corresponds to what we print in console.log

However shouldn’t this go as is into the body of the JSON while doing the call ?

We do it in JS because we need to make a list of JSON objects of the format: {“label”: SKILL_NAME}, with SKILL_NAME being the name of different fields in our database.
I didn’t saw any functionality in Bubble to do something like this, but maybe I’m wrong, I only started using Bubble very recently…

You do it like I said using format as text.
You first load the list of “label” (Search for skills) and you do: format as text.
In format as text, you will do {“label”:“this skills label text field”} and you will have a field to concatenate using , in this case.
I don’t see any issue with the current structure of choices from your JS.
What you can test, is to use a requestbin endpoint. Send the request from the backend directly and after try to send it using your JS (or the format as text method I’ve suggested).
compare both

I couldn’t figure out how to make it with JS correctly… Don’t know where the problem was (and to lazy to find out) I used the format as text and it worked as intended. Thank you a lot for your help.

1 Like

Thanks a lot Jici, you’re a rockstar!

1 Like