API response containing escaped JSON?

Hi,

The API I’m using returns a nested array response in escaped JSON, for example:

[\r\n {\r\n \"id\": 1,\r\n \"name\": \"John Doe\",\r\n \"email\": \"john.doe@example.com\",\r\n \"age\": 30,\r\n \"isStudent\": false\r\n },\r\n {\r\n \"id\": 2,\r\n \"name\": \"Jane Smith\",\r\n \"email\": \"jane.smith@example.com\",\r\n \"age\": 25,\r\n \"isStudent\": true\r\n },\r\n {\r\n \"id\": 3,\r\n \"name\": \"Robert Brown\",\r\n \"email\": \"robert.brown@example.com\",\r\n \"age\": 22,\r\n \"isStudent\": true\r\n },\r\n {\r\n \"id\": 4,\r\n \"name\": \"Linda White\",\r\n \"email\": \"linda.white@example.com\",\r\n \"age\": 28,\r\n \"isStudent\": false\r\n },\r\n {\r\n \"id\": 5,\r\n \"name\": \"Michael Green\",\r\n \"email\": \"michael.green@example.com\",\r\n \"age\": 32,\r\n \"isStudent\": false\r\n }\r\n]\r\n

When I try to setup the API call, it doesn’t detect the JSON array (list), presumably because of the escaped JSON. Is there a solution to this? How can I get the API to correctly detect the list?

Thank you,
Mikhaeel

Can you provide url to API Doc?
How did you set API Connector?

Hey, thanks for your reply.

It’s actually Open AI’s GPT. I’ve instructed it to respond to my query in JSON, but because it’s nested in the JSON response, it’s escaped.

Here’s the full API response:

There’s a lot of topic about this API and this response. Basically, the API return a string that contain a JSON. You will probably find solutions in other topic.

One can be to use one of the JSON tools available in plugins marketplace.
Another is to send the JSON to a backend WF and let Bubble parse it. You need to get the same JSON structure each time.

I can parse it afterwards. In fact, it’s pretty easy to un-escape the JSON with a simple find & replace operation. However, my problem is that I need to use the array (list) that is returned as the data source for a repeating group. I don’t believe I can do that if I parse that response afterwards? It has to be detected as a list from the API connector.

a) send the json to backend WF using API connector and send it as text. Use return data from API. Put the whole json text in a raw response as JSON.
B) save the json to file. Use api connector to call the file url.

Interesting idea. I could try something like that. It might be a little inefficient, but I don’t think it matters much.

Thanks for the suggestion! I’ll give it a shot.

1 Like

Did you ever make this work? I have the exact same problem … cannot iterate through list I get back from chat completion …

Certainly! Below is an example of an API response containing escaped JSON:

{
    "status": "success",
    "data": "{\"user\": {\"id\": 1, \"name\": \"John Doe\", \"email\": \"john.doe@example.com\"}, \"messages\": [{\"id\": 101, \"text\": \"Hello, John!\"}, {\"id\": 102, \"text\": \"Your order has been shipped.\"}]}"
}

In this example, the data field contains a string that is an escaped JSON object. The backslashes (\) are used to escape the double quotes within the JSON string. When parsed, the data field will be interpreted as a nested JSON object.