Create a JSON string for an API

Hello,

I have been days trying to set up an API call with Uber Direct. This API call needs to receive the pickup address and dropoff address as JSON string like for example:
"{\"street_address\":[\"100 Maiden Ln\"],\"city\":\"New York\",\"state\":\"NY\",\"zip_code\":\"10023\",\"country\":\"US\"}",

I tried using static values for the API call and it worked fine, the problem comes when trying to set dynamic values for the API call. In order to build a JSON string like the one above I need to use arbitrary text, the problem is that Bubble pass these arguments as _text of type _text rather than _text as JSON which is making the API call to fail.


(This is an email that bubble support sent me regarding this problem)

Does anyone know what to do? I have tried using arbitrary text and also arbitrary text: formatted as JSON-safe but none of them worked

{
  "street_address": ["Parent group's Seller's Address:extract street",],
  "city": "Parent group's Seller's Address:extract city",
  "state": "Parent group's Seller's Address:extract state",
  "zip_code": "Parent group's Seller's Address:extract postal code",
  "country": "US"
}
:formatted as JSON-safe

I also tried this which created a good JSON string which I displayed in a text element but when I set it as dynamic value in the API call it does not work.

Thank you in advance.

Look, I’m not sure there’s a silver bullet to solve this problem, but I’ve run into it a few times myself, and I know how frustrating it can be. I’ll share two solutions that have worked for me more than once — hopefully, one of them might help you too.

The first one is the simplest, and it has saved me several times: add a Find & Replace after the Arbitrary text, and in the “Find” field, insert only the line break (Enter/Return) character, leaving the “Replace” field empty. This way, Bubble removes all extra line spacing from the text.

The second approach is to structure the entire JSON directly in the body of the API call, formatting it with escaped values and using only the parameters inside < > to be filled in on the front end. Keep the quotes inside the body of the call. Then, in the Arbitrary text, apply the same Find & Replace as mentioned above, and add one more Find & Replace — this time replacing quotation marks (") with nothing. This helps prevent quotes from appearing in the middle of the text.

These tips might not apply perfectly to your case, but they’ve solved the issue for me multiple times, so I think they’re worth a try.

This is the correct way to do that (if you put the json part in arbitrary text and also, I think I’ve recommended to remove all newline to avoid issues)
After, if your json doesn’t work, did you use debugger tool (inspect code, send request to a requestbin and jsonlint the result?) Compare a request that work in POSTMAN and sent to requestbin VS the one sent by Bubble? This is how you will find what is not working.

Here’s my settings that work


You can dynamically populate address part

1 Like

I usually use formatted as json safe and use this find and replace with regex to remove the " that the formatted as json safe adds

There’s no reason to remove added "… if you don’t need them, don’t set them in the API connector. This make thing overcomplicated where there’s no reason for that.

Hello guys,

I finally got it! Thank you to all the replies. The solution that worked for me was to use arbitrary text and manually write the escaped string as seen in the picture, then using dynamic values. Since in the API body I have the pick_up_address and dropoff_address fields with " I did not have to add :formatted as JSON-safe after the arbitrary text so I can avoid double quotation.

1 Like

Again… this way to do thing could lead you to error in future if, for example, you need to have to escape a characters in the address city. This will not be handled by your settings while my solution will not fail for that… However, there’s not a lot of chance for this kind of fields.

The correct way to escape JSON in Bubble is to use (correctly) formatted as json-safe.

1 Like