Question
· 11 hr ago

How can i handle boolean JSON in IRIS?

Hi folks!

How can we handle JSON boolean types in IRIS?

I'm having a UI sending me a JSON payload as:

{

  "id": 1,
  "strprop": "string",
  "boolprop": true

}

The class is 

MyPackage.MyClass Extends (%Persistent%JSON.Adaptor)

{

strprop as %String;

boolprop as %Boolean;

}

And I'm trying to update an instance of a MyPackage.MyClass and I'm getting a datatype-related error, something like %JSONImportInternal+269...

How am I supposed to deal with booleans in JSON with IRIS and ObjectScript? Any best practice advices?

Product version: IRIS 2025.3
Discussion (4)3
Log in or sign up to continue

InterSystems IRIS handles JSON boolean values (true and false) differently than JSON. In ObjectScript, boolean values are represented as integers: 1 for true and 0 for false [1].

To correctly work with a boolean value from JSON, the following practices are recommended:

  1. Conversion During Object Interactions: When defining an object in JSON, you can set properties as "true" or "false" without issue, which will represent these values properly in a JavaScript JSON context. However, IRIS will convert these values to 1 or 0 when interacting via ObjectScript.

  2. Setting Boolean Values: If you manually set properties for an object using dot syntax in ObjectScript, you must use 1 or 0 for true and false, respectively, as IRIS will throw a syntax error for direct use of "true" or "false" outside the dynamic JSON context.

  3. Best Practice with %Set Method: Use the %Set() method to ensure the type of the values being set. You can specifically set a JSON boolean type by providing its corresponding type parameter as "boolean":

    Set obj.%Set("key", 1, "boolean")  ; Sets key to `true`
    Set obj.%Set("key", 0, "boolean")  ; Sets key to `false`
    

These approaches ensure compatibility and prevent errors when handling JSON boolean values in IRIS [1].

Sources:

Class MyPackage.MyClass Extends (%Persistent, %JSON.Adaptor)
{
 Property JSONid As %Integer(%JSONFIELDNAME = "id");
 Property strprop As %String;
 Property boolprop As %Boolean;
}
                                                                           

next this worked

set jsn={  "id": 1,  "strprop": "string", "boolprop": true }
set sc=obj.%JSONImport(jsn)
zw obj
+----------------- general information ---------------
|      oref value: 2
|      class name: MyPackage.MyClass
| reference count: 1
+----------------- attribute values ------------------
|       %Concurrency = 1  <Set>
|             JSONid = 1
|           boolprop = 1
|            strprop = "string"
+-----------------------------------------------------