Hi Rick,
I am trying to deserialize a json file that has embedded arrays. It's from openAi and the json deserialize function only reports out the level of the first array encountedin it seems in some instances. However with the following json file with embedded arrays encounters parsing errors. I see online there is a free JSONFox github code, maybe it handles this. I see a previous message made reference to this. Was wondering what suggestions you might have.
Using code:
local lcJsonFile
lcJsonFile = filetostr("test/listrunsresponse.json")
loJsonSerializer = Createobject("wwJsonSerializer")
lcJson = loJsonSerializer.deSerialize(lcJsonFile)
Here are the file contents:
{
"object": "list",
"data": {
{
"id": "run_abc123",
"object": "thread.run",
"created_at": 1699075072,
"assistant_id": "asst_abc123",
"thread_id": "thread_abc123",
"status": "completed",
"started_at": 1699075072,
"expires_at": null,
"cancelled_at": null,
"failed_at": null,
"completed_at": 1699075073,
"last_error": null,
"model": "gpt-3.5-turbo",
"instructions": null,
"tools": {
{
"type": "code_interpreter"
}
},
"file_ids": {
"file-abc123",
"file-abc456"
},
"metadata": {},
"usage": {
"prompt_tokens": 123,
"completion_tokens": 456,
"total_tokens": 579
}
},
{
"id": "run_abc456",
"object": "thread.run",
"created_at": 1699063290,
"assistant_id": "asst_abc123",
"thread_id": "thread_abc123",
"status": "completed",
"started_at": 1699063290,
"expires_at": null,
"cancelled_at": null,
"failed_at": null,
"completed_at": 1699063291,
"last_error": null,
"model": "gpt-3.5-turbo",
"instructions": null,
"tools": {
{
"type": "code_interpreter"
}
},
"file_ids": {
"file-abc123",
"file-abc456"
},
"metadata": {},
"usage": {
"prompt_tokens": 123,
"completion_tokens": 456,
"total_tokens": 579
}
}
},
"first_id": "run_abc123",
"last_id": "run_abc456",
"has_more": false
}
Thanks, Greg

That's invalid JSON and you don't have data
as an array. Here's what the validator in VSCode says:
data
should be an array not an object in that code - I don't think that code came directly from OpenAI - I've used their JSON and it parses fine.
The proper JSON should look like this:
{
"object": "list",
"data": [
{
"id": "run_abc123",
"object": "thread.run",
"created_at": 1699075072,
"assistant_id": "asst_abc123",
"thread_id": "thread_abc123",
"status": "completed",
"started_at": 1699075072,
"expires_at": null,
"cancelled_at": null,
"failed_at": null,
"completed_at": 1699075073,
"last_error": null,
"model": "gpt-3.5-turbo",
"instructions": null,
"tools": [
{
"type": "code_interpreter"
}
],
"file_ids": [
"file-abc123",
"file-abc456"
],
"metadata": {},
"usage": {
"prompt_tokens": 123,
"completion_tokens": 456,
"total_tokens": 579
}
},
{
"id": "run_abc456",
"object": "thread.run",
"created_at": 1699063290,
"assistant_id": "asst_abc123",
"thread_id": "thread_abc123",
"status": "completed",
"started_at": 1699063290,
"expires_at": null,
"cancelled_at": null,
"failed_at": null,
"completed_at": 1699063291,
"last_error": null,
"model": "gpt-3.5-turbo",
"instructions": null,
"tools": [
{
"type": "code_interpreter"
}
],
"file_ids": [
"file-abc123",
"file-abc456"
],
"metadata": {},
"usage": {
"prompt_tokens": 123,
"completion_tokens": 456,
"total_tokens": 579
}
}
],
"first_id": "run_abc123",
"last_id": "run_abc456",
"has_more": false
}
It looks to me when you copied the data you lost the []
brackets and converted them to {}
or maybe that's not JSON (Python structure maybe?).
The above parses correctly as it's valid data.
+++ Rick ---
Hi Rick,
I copied the data from a help file on the openai site. And then i ran the wwJsonSerializer.deserialize() function on the code. And the result was that it ran but did a limited conversion in to a resulting nested foxpro object. (The data i posted was, like you say, not the original data copies from the openai website helpfile at https://platform.openai.com/docs/api-reference/runs/listRuns I had edited the [] out to see if that changed the deserialization results. Sorry about that.
When i run the deserialization of the actual json data copied from openai help file, the result is as follows:
Which shows just the top level accessible in the nested object.
I copied the data from a help file on the openai site.
Well then the data on the doc site is wrong - it's invalid JSON. It looks like you got the right JSON to make it work - it will never work with invalid JSON.
Which shows just the top level accessible in the nested object.
The nested object is a FoxPro Collection... Doesn't anybody know how to use the native Collection
classes in FoxPro?
Collections don't show their contained items in the FoxPro debugger, so you have to set a watch for individual items loResult.Data[1]
or use some 3rd party tools from Tor to view the live structure.
But... you already have the structure in the JSON, so you don't need to look at the debugger - just look at the JSON.
+++ Rick ---

Hi Rick,
Thanks Rick for your patience. It all worked as you said. I guess the point is that this collection is programmably accessible and even if it needs to be accessed using the watch window to inspect to view it, the array can be accessed from the nested object in code. So looped code just has to account for the fact arrays/foxpro collections are also in the json file, and retrieve accordingly.
Thanks again, Greg