Struggling to have my API endpoint respond with a JOSN with two nested arrays

Hi, I created an endpoint where I’d like my user to get a JSON as the following

{
    "status": "success",
    "response": {
        "status": "success",
        "message_for_user": "Exemplary message for user",
        "vouchers": [
            {"voucher_id":"GJXCM","amount":"20"},
            {"voucher_id":"L5PTM","amount":"5"},
            {"voucher_id":"I5AQH","amount":"10"}
        ]
    }
}

After several attempts, the best I could do is the following.

  1. In case “vouchers” is set to a list of texts
{
    "status": "success",
    "response": {
        "status": "success",
        "message_for_user": "Exemplary message for user",
        "vouchers": [
            "{\"voucher_id\":\"GJXCM\",\"amount\":\"20\"}",
            "{\"voucher_id\":\"L5PTM\",\"amount\":\"5\"}",
            "{\"voucher_id\":\"I5AQH\",\"amount\":\"10\"}"
        ]
    }
}
  1. In case “vouchers” is set to a unique text
{
    "status": "success",
    "response": {
        "status": "success",
        "message_for_user": "Exemplary message for user",
        "vouchers": "{\"voucher_id\":\"GJXCM\";\"amount\":\"20\"},{\"voucher_id\":\"L5PTM\";\"amount\":\"5\"},{\"voucher_id\":\"I5AQH\";\"amount\":\"10\"}"
    }
}

Bubble is adding some unwanted backslashes. I tried to use “find and replace” to get rid of them but it doesn’t work. I also checked several posts in the forum but I couldn’t find anything.

I can have full control of the returned body in case I set content-type = plain text but I need my users to get a Json.

Thanks in advance for your help!

P.S. I’m not a developer

@keith I know you’re super expert on this and much more. Can you help? :slight_smile:

@keith will probably ot answer. Can you share screenshot of what you have set?

1 Like

Hi @Jici thank you for your reply.
my endpoint is named get_vouchers and needs the variables “account” and “phone”.

as I’m testing different methods, it triggers a custom WF named “test3”

Test3 creates a new thing in a data type I’m normally using for another topic so don’t mind about the names of the fields.
The first step sets a list of transactions coming from a research. It works.


Step 2 fills another field of the same thing (a list of texts) with the result of Step 1 after using format as text where I populate the 2 variables “voucher_id” and “amount” . Note that I separate them for the moment with a “;” (this is because otherwise they get separated in following steps). It works and I see the following in the db:

Step 3 returns data after a first find and replace replacing “;” with “,”. The following find and replace are for deleting \n and, afterwards, \. Such last two replacements are useless because the backslash is added by bubble at a later moment.


As a last step, the api returns the output that appears as follows:

{
    "status": "success",
    "response": {
        "status": "success",
        "message_for_user": "Exemplary message for user",
        "vouchers": [
            "{\"voucher_id\":\"GJXCM\",\"amount\":\"20\"}",
            "{\"voucher_id\":\"L5PTM\",\"amount\":\"5\"}",
            "{\"voucher_id\":\"I5AQH\",\"amount\":\"10\"}"
        ]
    }
}

Thanks in advance for your suggestions.

The problem is that you store the vouchers list in a list of text and you use structured json. Even if you have set this as a JSON… in fact, this is a string.

What I believe is that you shouldn’t try to encode the JSON before and store it in a list of text, but instead generate the needed JSON in the return data from API directly, and instead of using Structured JSON, use other content-type. Add you own content-type for application/json and structure your JSON directly in the response.

1 Like

@Jici I solved!!! :tada::tada::tada::tada:

I’m explaining here the solution in case other bubblers need it.

With content-type Structured JSON:

  • on one side it appears that I cannot “fool” the system creating separate objects by just playing with the syntax.;
  • on the other side, it seems that Bubble doesn’t allow to have a list of lists.

What led me to the solution is your comment “use other content-type” :pray:

By selecting “Other content-type” and typing “JSON” I could first paste an exemplary json and check that Postman was reading it correctly. Afterwards I filled my text with the needed content. In particular:

  1. in a first step I created my list using the syntax below

  2. in a second step I retuned the data as follows:

  3. This is the result in Postman:

Thank you!

1 Like