Building an api using backend workflows and customising response

Problem:
Client wants a JSON file of recent orders (carts). There are cart items within each cart. This client has cart items associated to rooms in the house (they are a building merchant) and when we send the orders to the clients api they want the cart items grouped into rooms. How do I achieve this? On the front end I achieve this by have a repeating group inside a repeating group (rooms, with their cart items) but I can’t find something similar when creating an api response

JSON array should look like

“room”: “room name”
[items array]
and so on
“room”: “room name”
[items array]

Current workings:

Example data structure for cart items: cart item, product, qty, room

Screenshot

I can get a list (array) of all the rooms and this then lists all the cart items. But this doesn’t include the SKU and QTY see below

I can get a list of all the cart items with the room configs as meta data, but the list of items See below

2 Likes

So…

{
"rooms : [
{“roomname” :“room1”,
“items”: [{“sku” : “122”, “quantity”,1},{“sku” : “123”, “quantity”,1}],
{“roomname” :“room2”,
“items”: [{“sku” : “222”, “quantity”,3},{“sku” : “223”, “quantity”,1}],
]}

?

An Array of item objects in an array or room objects?

Or an array of values?

2 Likes

Yes this setup. An array of item objects in an array of room objects. And rather than returning all the key : values within the objects i want to return only some and their childen.

for example

a cart item (object) has a product (object). I want to return the product’s SKU rather than the uniqueid of the product object

Hope this makes sense

It does. So what I think you will need to do is build a text field on the room (and on the items) which has the structure …

{“roomname” :“room1”, “items”: [{“sku” : “122”, “quantity”,1},{“sku” : “123”, “quantity”,1}]

…you can use “formatted as list” to get the list output with the commas.

So each item will also need a JSON formatted field of {“sku” : “122”, “quantity”,1}

Then in your Return to API have a single key of rooms, and list out the room’s JSON fields using :formatted as list

If you search for PostMark App on here you will see I use a very similar thing for creating JSON list to pass to my mail apps.

However … if you need an example please get back to me !

AFAIK you can’t structure nested objects/arrays in the native Bubble “Return data to API”.

3 Likes

Are you trying to make a JSON file of each users cart item?

This some interesting topic.

Does anyone know of a course or something so that you can create an API so other services connect to your Bubble app? For example, what if I want that all the users that run their online shop in Woocommerce to send the customers and the sales made to the Bubble app?

Awesome. I’m going to try this approach and hopefully work through to solution

1 Like

No, but I can make one :slight_smile:

2 Likes

I think that would be awesome.

Let me know when you have it so we can buy it.

This is something super powerful and not much information in the forum.

I mean this extends our apps a lot.

Yeap, I think Nigel has got a solution which I’m going to try out today

As Nigel also wrote, it doesn’t look like we can structure nested objects/arrays natively. This is def a good use case for a plugin

I’ll help :wink:

I agree

I was really struggling to find a good answer/obvious solution. I thought this was a typical use case.

I figured one way to do it was to pass the data to integromat and do the nesting there but storing the json in a text field does it.

Weirdly I’d already added this plugin JSON Machine Pro Plugin | Bubble hoping this would let me nest. It doesn’t, but it does allow me to write the json to a text field

1 Like

Hey nigel, I thought I could do it but i’m getting stuck

I’ve done the cart items part

and created the room json

image

Q) do I need update from the room json field each time an item is added/removed from the cart. As the current structure is not dynamic

Q) and therefore, is the API response only fetching from the room datatype and not going ‘down’ to the cartitems json?

Maybe an example might help

Another solution is by using API Connector:

let extractedData = JSON.parse( ‘[’ + properties.thing1 + ‘]’)
extractedData

As you can see all fields MUST be prefixed by _ api_c2_ which seems to stand for “api connector 2”

  • Set the return type as a list of your fake API type

  • Then create your “Return data from API” setting an entry as a list of this fake API type and as value the returned values of your Server Script

One caveat, in my opinion, is that all your fields must be prefixed with this _ api_c2_.
Another one is for Option Sets which I have only been able to export as text.
Also I haven’t tried by nesting types to make multi-dimensional JSON structures.

1 Like