Page 1 of 1

Basic JSON stuff

Posted: Fri Jan 12, 2024 5:20 pm
by Seymour Clufley
I'm struggling to get to grips with the JSON library. Let's say that I have a JSON like this:

Code: Select all

[
  "saveObject",
  "task8nv7a6o",
  "{\"text\":\"tedrt227\",\"project\":\"projpfrzfw5\",\"sequel\":[],\"dl_created\":\"2023-08-16T03:34:14.000Z\",\"dl_deadline\":\"2024-01-20T11:24:51.000Z\"}"
]
How would I extract the third item here and save it as a separate JSON?

Re: Basic JSON stuff

Posted: Fri Jan 12, 2024 6:34 pm
by spikey
The sample contains a JSON array. You could extract the array and then reparse the embedded section:

Code: Select all

Dim a$(0)

LoadJSON(0, "test.json")

Value = JSONValue(0)
ExtractJSONArray(Value, a$())

ParseJSON(1, a$(2))
Debug ComposeJSON(1, #PB_JSON_PrettyPrint)
Or, if you've no interest in the remainder of the array, you can access the array element directly:

Code: Select all

LoadJSON(0, "test.json")

Value = GetJSONElement(JSONValue(0), 2)

ParseJSON(1, GetJSONString(Value))
Debug ComposeJSON(1, #PB_JSON_PrettyPrint)

Re: Basic JSON stuff

Posted: Fri Jan 12, 2024 9:01 pm
by Seymour Clufley
That's perfect, Spikey. Thank you very much.