Written by

Senior Startups and Community Programs Manager at InterSystems Corporation
Question Evgeny Shvarov · Jan 18

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

Comments

DC AI Bot · Jan 18 Bot

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:

0
Yuri Marx · Jan 18

Write the id property into your objectscript class, the boolean it is ok

0
Robert Cemper · Jan 18
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"
+-----------------------------------------------------
0
Robert Cemper  Jan 18 to Robert Cemper

If your payload is received as a string 

set jsn={}.%FromJSON(payload)
write jsn.%Get("boolprop")

works without a persistent object

0
Evgeny Shvarov · Jan 19

Thank you, @Yuri Marx @Robert Cemper ! Works for me as well - my issue was that JSON.Adapter is case sensitive. if payload comes as:

{  "strprop": "string",   "BoolProp": true

error fires (very unreadable). I thought that reason is the boolean value, but turned out it is property naming issue - should 100% match either name in a class description, or special JSON property name clause.

0