Published on InterSystems Developer Community (https://community.intersystems.com)

Home > JSON - NULL Properties

Question
Justin Richter · Jan 3, 2022

JSON - NULL Properties

So I've been reviewing a lot of questions posted in the InterSystems community regarding NULL properties in JSON. I've also been reviewing the JSON documentation. None of these things have been able to help me so far.

1. We don't seem to have the %JSON.Adaptor class available for us to use in our system.

2. I'm not really confident enough to create JSON Type classes or backporting code, etc.

 

I created a dummy class and I need my JSON to look like this:

{
    "notanumber":"28001",
    "aboolean":true,
    "anumber":12345,
    "adecimal":1.2,
    "adate":"01/01/2023",
    "adate2":"01-01-2023",
    "anull":null,
    "anull2":null,
    "anull3":null
}

Instead of using empty strings like this:

{
    "notanumber":"28001",
    "aboolean":true,
    "anumber":12345,
    "adecimal":1.2,
    "adate":"01/01/2023",
    "adate2":"01-01-2023",
    "anull":"",
    "anull2":"",
    "anull3":""
}

 Here is a dummy class that I created to get that output and run various tests with:

Class HchbLib.Testing.DummyClass Extends %RegisteredObject
{

Property notanumber As %String;

Property aboolean As %Boolean;

Property anumber As %Integer;

Property adecimal As %Integer;

Property adate As %Date;

Property adate2 As %Date;

Property anull As %String;

Property anull2 As %String;

Property anull3 As %String;

Method outout2JSON()
{
    set tBody = ##class(%GlobalCharacterStream).%New()
    //do ##class(Ens.Util.JSON).ObjectToJSONStream(##this,.tBody,"e")
    do ##class(Ens.Util.JSON).ObjectToJSONStream(##this,.tBody,"eiws")    
    w tBody.Read()
    quit $$$OK
}

ClassMethod Test()
{
    
    set dummy = ..%New()
    set dummy.notanumber = "28001"
    set dummy.aboolean = 1
    set dummy.anumber = 12345
    set dummy.adecimal = 1.2
    set dummy.adate = "01/01/2023"
    set dummy.adate2 = "01-01-2023"
    set dummy.anull = $$$NULLOREF
    set dummy.anull2 = ""
    do dummy.outout2JSON()
    quit $$$OK
}

}

 

After you compile it, you can obviously call it using:  do ##class(HchbLib.Testing.DummyClass).Test() in Studio.
 

Thanks for your help everyone!

#Data Import and Export #JSON #ObjectScript #Caché #Ensemble #HealthShare

Source URL:https://community.intersystems.com/post/json-null-properties