Page 1 of 1

Question on JSON handling

Posted: Sun Apr 30, 2023 1:41 am
by lesserpanda
Hi guys,

I'm trying to parse and get posted JSON values easily.

Here's an excerpt of what I'm doing

Code: Select all

JSON_ID = ParseJSON(#PB_Any, SomeStringReceived)
If JSON_ID
	If GetJSONMember(JSONValue(JSON_ID), "Target")
		Target.s = GetJSONString(GetJSONMember(JSONValue(JSON_ID), "TargetType"))
	EndIf
EndIf
This fails when the Target is not a string and say it's an integer.

How do I catch this exception and handle it?

Re: Question on JSON handling

Posted: Sun Apr 30, 2023 2:33 am
by jassing
try JSONType()

Re: Question on JSON handling

Posted: Sun Apr 30, 2023 2:46 am
by Demivec
lesserpanda wrote: Sun Apr 30, 2023 1:41 am Hi guys,

I'm trying to parse and get posted JSON values easily.

Here's an excerpt of what I'm doing

Code: Select all

JSON_ID = ParseJSON(#PB_Any, SomeStringReceived)
If JSON_ID
	If GetJSONMember(JSONValue(JSON_ID), "Target")
		Target.s = GetJSONString(GetJSONMember(JSONValue(JSON_ID), "TargetType"))
	EndIf
EndIf
This fails when the Target is not a string and say it's an integer.

How do I catch this exception and handle it?




Perhaps:

Code: Select all

JSON_ID = ParseJSON(#PB_Any, SomeStringReceived)
If JSON_ID
	If GetJSONMember(JSONValue(JSON_ID), "Target")
        If JSONType(GetJSONMember(JSONValue(JSON_ID), "Target")) = PB_JSON_String
	        Target.s = GetJSONString(GetJSONMember(JSONValue(JSON_ID), "TargetType"))
        Else
	        ;handle exception
        EndIf
	EndIf
EndIf

Re: Question on JSON handling

Posted: Mon May 01, 2023 2:22 pm
by lesserpanda
If JSONType(GetJSONMember(JSONValue(JSON_ID), "Target")) = PB_JSON_String

this is what I was after ... thank you