Is it possible to convert data types in Bubble?

I am working with an API call that returns a JSON object in an action.
When the page loads, I need to set this JSON object into a standardized structure.
This standardized structure is the same as the JSON object.

In normal programming you could just say JSONObject-VersionTest = JSONObjectDisplay and JSONObject-Live = JSONObjectDisplay depending on the version of the site you are on. However, I can’t get this logic implemented because the expressions won’t evaluate.

I want to do this in order to allow for an API call on version-test and a different one from the live site* to load the same content into the page.

*I would use live/dev keys but the API connection is ‘special’ in that it is using the Auth0 Login Flow which requires a token. The API connector is limited so that it can’t obtain the token dynamically (it will work on one side or the other because it is hard-coded / not currently possible to dynamically switch. This is confirmed by Bubble support and over two weeks of banging my head against a wall).

I am so stuck. Please help!

1 Like

I am in the same boat, two different API calls return the same structure but Bubble treats them as two different data types.
I’ve been racking my brain trying to figure out a way but haven’t found a solution yet.
Maybe rewriting the logic behind the API could help (if you control it, like Xano), like if you give it parameter A then it would return this set of data, if you give it parameter B then it’d return another set of the same data, but because the name of the API would be the same Bubble would think it’s the same dataset?

@mc3digital OK, what if you create 2 custom states for the page with the type of JSONObjectDisplay, like “versionTest” and “versionLive” and add the returned data to both, depending on which version you pull data from with the API. Then you’d be able to exchange data between the two custom states since they are the same data type. Would that help?

I can give feedback on the structure, but for the most part they are the same structure. Sometimes the staging api will have more data in it. The goal of this is to prepare the site structurally to deploy changes. Then when it goes live, it would just call a different api but generally pull the same info and work the same.

This is close to what I am doing. Loading the JSONObjectDisplay will fail because of the data type mismatch on one side or the other though.

Currently I got it working by loading the API return into a bunch of custom states. Since most of them contain arrays, I am using each item’s value to load lists. Then the repeating group is set to text as the types and datasource is one of the lists. It is a bit awkward but I reference each custom states item # set to the current row to output each value. It worked though it really adds a lot of work to the process and I am a bit uneasy about loading lists since a lot of the arrays have null values. it does appear to load each list properly (it will show 1,2,1,0) thankfully so it does work.

I was wondering if I could do something in toolbox with the javascript map function. That might allow me to map the new variable type, but I am unsure if that would return a valid JSON object the page could understand.

Url for API calls can be dynamic, on the page you can use “isnt live version” to specify which one to use on live or dev. It will be the same api, but if you have any parameter that can define is it test or not, it will work

1 Like

Yes, this is useful and I am using this in part of the solution.

However, this api requires authentication ( including a manual login process ) it isn’t that simple to just switch calling a different API that is the issue.

Either way, because of this the data types won’t evaluate on one side or the other.

So the JSONObjectDisplay would sometime return with a different structure?

Yes, sometimes there would be more items in the testing side before it is deployed to the server.
Version test gets built up using this structure.
[ { item1 = "Value1", item2 = "Value2", item3 = "Value3" } ]
Current return in live site:
[ { item1 = "Value1", item2 = "Value2" } ]

  • There is a lot of arrays, some with 10-20 items in them.

Once the testing is complete, they push the API to the live environment and I map conditionals to load the values.

I have been able to make progress setting a large number of lists as custom states as mentioned in a previous post. Still looking for a better approach as it has been difficult to manage the solution.

That is bad if the same API returns with different data structure. I don’t know any solution for that.
Just a crazy idea, would that be possible to return the data as a string and dissect it with Regex or something, instead of returning varying data structures?

It’s the same structure, just different amount of items per list.

Maybe my example doesn’t spell it out well, but they are not the same structure. Item3 is an additional variable added to each of the lists that would show up in the array.

Even if they were exactly the same, I would still be stuck because I can’t convert between data types in Bubble.

It isn’t bad, the API is simply under development and new data points are being added to the existing one.

That is what I am wondering about. I am not that good with regex, but was thinking I could do something with Toolbox possibly.

This topic was automatically closed after 70 days. New replies are no longer allowed.