API posting an array object

Hi

Reaching out to the amazing brain trust of bubblers

I am posting an array of objects in an API post as below (snipet). If I want to send 10 objects in the array do I have to add 10 dynamic objects into the JSON call? Does bubble not support a dynamic way of doing this?

    "industries": {
			"values": [{
				"code": "<industry_code>",
				"type": "<industry_type>"
			},
{
				"code": "<industry_code2>",
				"type": "<industry_type2>"
			}]

Thanks

Simon

2 Likes

So all you need do is build a list of texts (or anything that can resolve to a list of texts, like Some_things's Name where a Some_things is a list of things and Name is a text field on that).

You’ll note that, when you convert a list of texts to its text representation (like print it out in a text element), it looks like this:

text1, text2, text3, text4, etc

by default.

So such a list is almost ready for insertion into a JSON structure “”, except that we need to delimit each text element. Instead of just a comma – , – we need ", " so that we would get (inside the JSON:

"text1", "text2", "text3", "text4"

(The bold " come from “”, and so we need the ", " part from somewhere else…)

Luckily, the :joined with operator for texts lets us do this:

Some_thing's Name:joined with ", "

Will return to us:

text1", “text2”, “text3”, "text4

And as you can see, this is ready for insertion in the part of your JSON body API Connector setup.

3 Likes

Hi Keith,

Thanks so much for taking the time. It makes perfect sense.

I tried it on my API call to initialize it but it doesn’t like it. So not sure I quite get it yet

savage%20dragon%20%20%20Bubble%20Editor

OH… additionally, one must sometimes indicate array by square brackets, right? What would be helpful in your screenshot above is a full picture of your body JSON object.

I don’t know what API this is, but including a sample call – or you just LOOKING at the sample call – will show you how you need to delimit / describe the arrays.

The other thing that’s super-handy when designing this stuff is to use a JSON validator (like jsonlint.com) to avoid bonehead syntax errors.

Bubble’s list functions don’t work so well with converting a list of objects to JSON.

You’re better off having no parameters. When making the call, construct the JSON body as a text including the list objects.

If you’re collecting the data on client-side, from database things, you can build the JSON with Toolbox List Item Expression.

If you’re collecting the data from javascript, you can get it back to Bubble with Javascript To Bubble.

If you’re makign the API call on a API workflow, you can use Server Script.

Examples linked to here:

2 Likes

Thanks Mishav

Hey Mishave, I know this is old, but what did you mean when you said we’re better off not having parameters? I’m currently trying to send a list of objects to a REST API I control, and if I’m not sending this list in a parameter, then how is it getting sent?

So I have figured out how to send a list of bubble things to my API. … it’s basically as @keith layed out before with the bit about :joined with ","

image

On the API/Plugin call side of things, make sure that your list parameters are being sent in as long values in a POST request . This way, these parameters will be sent in the body of the request which allows you to send a maximum of 10MB of data (this is in contrast to sending parameters in a GET request which sends parameters as query strings which can not be nearly as large).

Cheers!

5 Likes

@mishav thanks for the example…

Thank You. Documentation don´t show It!