Getting JSON data from ChatGPT API call

Hi there,

I’ve using a Python Dat to make an API call to ChatGPT, then using json.dumps to get the response into a JSON dat. Unfortunately the formatting of the ChatGPT response is causing an error that the object type is not JSON serializable.

I can see that the ChatGPT response does contain a section of properly formatted JSON text within {}, but also some extra non-formatted text at the start and end.

I’m a total novice with Python and JSON, but is it simply a case of removing the superfluous text? Could anyone guide me on this?

Or do I need to take a different approach?

Thanks!

Hi @kokosing,

can you append a file that has a response in it - might be easier to give you hints seeing the actual data.
Not sure if this is the case, just make sure to sanitize the json from any personal data if any.

cheers
Markus

1 Like

Sure:

ChatCompletionMessage(content=‘{\n “colors”: [\n {\n “name”: “Red”,\n “rgb”: [255, 0, 0]\n },\n {\n “name”: “Green”,\n “rgb”: [0, 255, 0]\n },\n {\n “name”: “Blue”,\n “rgb”: [0, 0, 255]\n },\n {\n “name”: “Yellow”,\n “rgb”: [255, 255, 0]\n },\n {\n “name”: “Purple”,\n “rgb”: [128, 0, 128]\n }\n ]\n}’, role=‘assistant’, function_call=None, tool_calls=None)

Hi @kokosing,

if you would be using a Web Client DAT to make the API calls, you could parse the message in the DAT’s callbacks. it looks like currently you get a larger object (‘ChatCompletionMessage’) but still need to get to it’s json content. You also would want to get rid of all the \n newlines…

What script are you using to make the API call?

cheers
Markus

1 Like

Yes, I’m trying to kill the \n 's as well!

This call is in a text dat (set to python) and is using the newer OpenAI API:

completion = client.chat.completions.create()

The text I pasted is the result of:

print(completion.choices[0].message)

Sorry, I should point out there’s a python module for OpenAI, which I’m importing.

Hi @kokosing,

can you get to the actual json by doing somethig like this:

import json
message_content = completion.choices[0].message['content']

# Convert the content to a JSON object
json_content = json.loads(message_content)

cheers
Markus

So, basicly you get a responseObject abck which contains more information then you need, and what you printed is basicly just a representation of this ChatCompletionMessage Object.
From what it looks like the ChatCompletionMessage Object as a “content” attribute, which you can access via a simple dot-access.
So instead of just planting the responseObject in to a dat

op("text1").text = ChatCompletionMessage

try the following

op("text1").text = ChatCompletionMessage.content

Then, the JSON-Parser will take care of all the newLines n stuff. No need to take hand in this.

Hi @alphamoonbase and @snaut Thank you both for your help!

I tried both options. Simply adding .content to the end was the key.

I really appreciate the help! As this is only one step in a bigger project (where I’ve got a lot to learn) I’ll probably be back with more questions soon.

Thanks again!