Functions such as ExtractJSONxxxx() can be used for this purpose.
Can you give a more detailed example of your JSON structure?
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
I want to get the type field if it exists, check it, and if it's 1 for example, extract the JSON fields for the name and message. But if I just extract them with GetJSONString(GetJSONMember(JSONValue(0), "name")) it crashes if the field doesn't exist. I thought about extracting to a map and using FindMapElement() but that seems almost more messy.
Structure MEMBERDETAIL
type.q
name.s
message.s
EndStructure
Define.MEMBERDETAIL Member
JSON.s="{"+Chr(34)+"type"+Chr(34)+": 1, "+Chr(34)+"name"+Chr(34)+": "+Chr(34)+"Quin"+Chr(34)+", "+Chr(34)+"message"+Chr(34)+": "+Chr(34)+"Hello world!"+Chr(34)+"}"
; UnescapeString seems corrupted on PB610bX
ParseJSON(0,JSON)
ExtractJSONStructure(JSONValue(0),@Member.MEMBERDETAIL,MEMBERDETAIL)
With Member
If \type=0 ; Or any other test
ClearStructure(@Member,MEMBERDETAIL)
Else
Debug \type
Debug \name
Debug \message
EndIf
EndWith
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
wombats wrote: Thu Jan 18, 2024 4:08 pm
GetJSONMember will return 0 if the member doesn't exist, so you can use it to check if a member exists, unless I'm missing something.
Structure MEMBERDETAIL
type.q
name.s
message.s
EndStructure
Define.MEMBERDETAIL Member
JSON.s="{"+Chr(34)+"type"+Chr(34)+": 1, "+Chr(34)+"name"+Chr(34)+": "+Chr(34)+"Quin"+Chr(34)+", "+Chr(34)+"message"+Chr(34)+": "+Chr(34)+"Hello world!"+Chr(34)+"}"
; UnescapeString seems corrupted on PB610bX
ParseJSON(0,JSON)
ExtractJSONStructure(JSONValue(0),@Member.MEMBERDETAIL,MEMBERDETAIL)
With Member
If \type=0 ; Or any other test
ClearStructure(@Member,MEMBERDETAIL)
Else
Debug \type
Debug \name
Debug \message
EndIf
EndWith
Oh, a structure! This is exactly what I was looking for, thanks!