Page 1 of 1

Rest API / JSON boolean type

Posted: Tue Oct 01, 2024 8:45 am
by tatanas
Hello,

I am currently testing REST commands with the API of a UNIFI network controller. In order to modify part of its configuration, I needed to change a JSON value of boolean type. I created a structure to accommodate the modifications, but I encountered problems when I wanted to send the content of my boolean variable. The controller did not accept the value I was sending. In the code below, you can see that the variable "mac_filter_enabled" was tested as both an integer and a string type, but that did not work. I had to create the payload manually by specifying true or false without it being considered a string.

Is this normal?
Could I have done it differently?
Does the problem come from the controller, which should understand that 1 or 0 in this variable corresponds to true or false?

Thank you in advance for the clarification.

Code: Select all

; NON FUNCTIONAL PAYLOAD
Structure macfilter1
	mac_filter_enabled.s ; <- boolean type variable STRING
	mac_filter_policy.s
	List mac_filter_list.s()
EndStructure
Define PutData1.macfilter1

PutData1\mac_filter_enabled = "true"
PutData1\mac_filter_policy = "deny"
AddElement(PutData1\mac_filter_list())
PutData1\mac_filter_list() = "AA:BB:CC:DD:EE:FF"
If CreateJSON(0)
	InsertJSONStructure(JSONValue(0), @PutData1, macfilter1)
	JASON_Payload1$ = ComposeJSON(0)
	FreeJSON(0)
EndIf
Debug JASON_Payload1$


; NON FUNCTIONAL PAYLOAD
Structure macfilter2
	mac_filter_enabled.i ; <- boolean type variable INTEGER
	mac_filter_policy.s
	List mac_filter_list.s()
EndStructure
Define PutData2.macfilter2

PutData2\mac_filter_enabled = #True
PutData2\mac_filter_policy = "deny"
AddElement(PutData2\mac_filter_list())
PutData2\mac_filter_list() = "AA:BB:CC:DD:EE:FF"
If CreateJSON(0)
	InsertJSONStructure(JSONValue(0), @PutData2, macfilter2)
	JASON_Payload2$ = ComposeJSON(0)
	FreeJSON(0)
EndIf
Debug JASON_Payload2$


; FUNCTIONAL PAYLOAD
Expected_JASON_Payload$ = "{" + 
            #DQUOTE$ + "mac_filter_enabled" + #DQUOTE$ + ":" + "true" + ", " +  ; <- boolean type
            #DQUOTE$ + "mac_filter_policy" + #DQUOTE$ + ":" + #DQUOTE$ + "deny" + #DQUOTE$ + ", " + 
            #DQUOTE$ + "mac_filter_list" + #DQUOTE$ + ": [" + 
            #DQUOTE$ + "01:02:03:04:05:06" + #DQUOTE$ +
            "]" +
            "}"
Debug Expected_JASON_Payload$

Re: Rest API / JSON boolean type

Posted: Tue Oct 01, 2024 9:30 am
by infratec
That's one of the problems of PB with JSON: there is no boolean in PB

You have to replace 0 by false and 1 by true in the resulting JSON text.
And a boolean in JSON is no string, so you need no " arround true and false.

Re: Rest API / JSON boolean type

Posted: Tue Oct 01, 2024 10:09 am
by infratec

Code: Select all

JASON_Payload2$ = ReplaceString(JASON_Payload2$, #DQUOTE$ + "mac_filter_enabled" + #DQUOTE$ + ":0", #DQUOTE$ + "mac_filter_enabled" + #DQUOTE$ + ":false")
JASON_Payload2$ = ReplaceString(JASON_Payload2$, #DQUOTE$ + "mac_filter_enabled" + #DQUOTE$ + ":1", #DQUOTE$ + "mac_filter_enabled" + #DQUOTE$ + ":true")

Re: Rest API / JSON boolean type

Posted: Tue Oct 01, 2024 10:27 am
by tatanas
Ok thank you.
That's what I did :)