Question on JSON handling

Just starting out? Need help? Post your questions and find answers here.
lesserpanda
User
User
Posts: 65
Joined: Tue Feb 11, 2020 7:50 am

Question on JSON handling

Post 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?
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Question on JSON handling

Post by jassing »

try JSONType()
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Question on JSON handling

Post 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
lesserpanda
User
User
Posts: 65
Joined: Tue Feb 11, 2020 7:50 am

Re: Question on JSON handling

Post by lesserpanda »

If JSONType(GetJSONMember(JSONValue(JSON_ID), "Target")) = PB_JSON_String

this is what I was after ... thank you
Post Reply