Basic JSON stuff

Just starting out? Need help? Post your questions and find answers here.
Seymour Clufley
Addict
Addict
Posts: 1266
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Basic JSON stuff

Post 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?
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
User avatar
spikey
Enthusiast
Enthusiast
Posts: 778
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Basic JSON stuff

Post 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)
Seymour Clufley
Addict
Addict
Posts: 1266
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: Basic JSON stuff

Post by Seymour Clufley »

That's perfect, Spikey. Thank you very much.
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
Post Reply