WagoAppJSON - default value if a sPointer isn't found?

The json I get using WagoAppJSON do not always include the sPointer “/0/carbonationTemp”.
If not found the value of “iBfCarbonationTemp” become 0 (zero). I want this to be 4 if the sPointer isn’t found.

If I declare “iBfCarbonationTemp” with the default value of 4 the code overwrite the value with 0 when I get the json.

		xBfJsonGetValueTrigger := TRUE;	
		typBfJsonGetValue := FbBfJsonParse.GetValueByPath(sPointer:= '/0/carbonationTemp', xTrigger:= xBfJsonGetValueTrigger);
		IF (typBfJsonGetValue.eReturn = JSON_SUCCESS) THEN
			iBfCarbonationTemp := TO_INT(typBfJsonGetValue.sValue);
			ELSE
				iBfCarbonationTemp := 4;
			END_IF
		END_IF

Anyone got a trick up their sleeve? :slight_smile:

This kinda works but I guess there are other ways to solve it as well…

		xBfJsonGetValueTrigger := TRUE;		// Carbonation temp
		typBfJsonGetValue := FbBfJsonParse.GetValueByPath(sPointer:= '/0/carbonationTemp', xTrigger:= xBfJsonGetValueTrigger);
		IF (typBfJsonGetValue.eReturn = JSON_SUCCESS) THEN
			iBfCarbonationTemp := TO_INT(typBfJsonGetValue.sValue);
		END_IF
		
		IF iBfCarbonationTemp = 0 THEN
			iBfCarbonationTemp := 4;
		END_IF

there is maybe several other ways but look at the case statement and try something like… (I did not test):

IF (typBfJsonGetValue.eReturn = JSON_SUCCESS) THEN
iBfCarbonationTemp := TO_INT(typBfJsonGetValue.sValue);
CASE iBfCarbonationTemp OF
0:
iBfCarbonationTemp := 4;
ELSE
iBfCarbonationTemp := TO_INT(sValue);
END_CASE
END_IF

2 Likes

hm, it should return eResult JSON_ERROR_INVALIDPOINTER if you trying to parse pointer not available in json string. Which version of WagoAppJSON are you using?

Mhm, that was my thought as well. I am using version 1.1.0.25