Parsing OpenAI API into structured JSON

I’m trying to figure out how to get Bubble to properly parse a response from OpenAI’s GPT API. Normally in Bubble a JSON response is automatically parsed and you can assign response contents to data types (things). In the GPT API, however, it lumps it as a single string.

I tried ChatGPT to help solve this and this is what it says:

The response you are receiving is a properly formatted JSON response from the OpenAI API. The response has an object called “body” that contains several fields, including “id”, “object”, “created”, “model”, and “choices”. The “choices” field is an array that contains a single object that represents the completed prompt.

The object within the “choices” array has several fields, including “text”, “index”, “logprobs”, and “finish_reason”. The “text” field contains the generated text response from the API in the form of a JSON string.

To convert the JSON string into a structured JSON object, you can use a JSON parser. The exact method for parsing the JSON will depend on the programming language you are using. Most modern programming languages have built-in JSON parsing libraries or modules that can be used to parse JSON strings into structured objects.

Anyone have an idea how to best parse the string and convert it to a structured JSON object to be able to save to the database?

Can you share the API Connector settings?

see my example how to store JSON response from OpenAI (or ChatGPT) to database :
Bubble-solutions | Bubble Editor

Add your API key and move to Use cases tab

As an update for anyone who is having similar issues, I was able to solve my issue using the following method.

  1. Used regex to extract the JSON portion of the response string from OpenAI API (resulting in just the JSON, stripping away everything else).
  2. Setup pythonanywhere web app with POST and GET api endpoints
  3. In my bubble workflow, after receiving the OpenAI API response, cleaning it with regex, then sent the JSON payload to pythonanywhere endpoint. PA receives the JSON, stores it in a db, then my bubble workflow sends a GET to PA to return the structured JSON that bubble will properly recognize so I can work with it in bubble.

My python web app is also doing some manipulation of the data that is easier to do there. A real pain, probably not the absolute best way to do it, but it works.

lot of round-trips there …

not great but i have called openai type api’s returning ( JSON.toString(response)) directly from javascript widget then, just use the javascript itself to parse && string process the json before calling the bubble wrapper to return from js to the UI runtimes next

in js box, just add some object manipulation to the following block , before calling the wrapper bubble_fn_Wrapper to return to normal bubble runtime

success => bubble_fn_uploadResp(JSON.stringify(success) )

By doing the above, the simplified string returned to bubble can more easily be handled with regex to make sense of the components of the string expression passed to bubble_fn_Wrapper

I have the same problem. Any ideas for solutions that work on the backend?

Sharing this thread which the grand @sat_miha helped me out on

1 Like