Question
Cristiane Ferre... · Dec 23, 2021

Send null in a string field (JSON)

I need to send the data from a view that returned NULL, but is going as empty. The fields are of type String.

I'm using: 

SET tSC  = ##Class(%ZEN.Auxiliary.jsonProvider).%WriteJSONStreamFromObject(.tSteamJson,pObj,,,,"e")
{
    "altura": "",
    "atendimento_id": "3060382",
    "data_atualizacao": "2021-12-16 10:39:12.0",
    "data_coleta": "2021-12-16 10:37:00.0",
    "data_liberacao": "2021-12-16 10:39:12.0",
    "dor": "1",
    "dor_nr_seq_result": "",
    "escala_dor": "",
    "escala_temp": "C",
    "freq_cardiaca": "80",
    "freq_respiratoria": "22",
    "glicemia_capilar": "",
    "ds_ritmo_ecg": "",
    "ds_decubito": "",
    "ie_derivacao_seg_st": "N",
    "ds_derivacao_seg_st": "",
    "nivel_consciencia": "",
    "pa_aparelho": "",
    "pa_diastolica": 80,
    "pa_manguito": "",
    "pa_sistolica": "120",
    "paciente_id": "903283",
    "peso": "",
    "profissional_id": "260",
    "prontuario_id": "903283-7",
    "sat_o2": "",
    "sat_o2_membro": "",
    "setor": {
        "id": "38",
        "nome": "7 A CLIN/CIR PEDIATRIA"
    },
    "temperatura": "36",
    "unidade_altura": "CM",
    "unidade_peso": "KG",
    "ie_o2_suplementar": "N"
}

I need

{
    "altura": null,
    "atendimento_id": "3060382",
    "data_atualizacao": "2021-12-16 10:39:12.0",
    "data_coleta": "2021-12-16 10:37:00.0",
    "data_liberacao": "2021-12-16 10:39:12.0",
    "dor": 1,
    "dor_nr_seq_result": null,
    "escala_dor": null,
    "escala_temp": "C",
    "freq_cardiaca": "80",
    "freq_respiratoria": "22",
    "glicemia_capilar": null,
    "ds_ritmo_ecg": null,
    "ds_decubito": null,
    "ie_derivacao_seg_st": "N",
    "ds_derivacao_seg_st": null,
    "nivel_consciencia": null,
    "pa_aparelho": null,
    "pa_diastolica": 80,
    "pa_manguito": null,
    "pa_media": null,
    "pa_membro": null,
    "pa_posicao": null,
    "pa_sistolica": "120",
    "paciente_id": "903283",
    "peso": null,
    "profissional_id": "260",
    "prontuario_id": "903283-7",
    "sat_o2": null,
    "sat_o2_membro": "",
    "setor": {
        "id": "38",
        "nome": "7 A CLIN/CIR PEDIATRIA"
    },
    "temperatura": 36,
    "unidade_altura": "CM",
    "unidade_peso": "KG",
    "ie_o2_suplementar": "N"
}
Product version: Ensemble 2012.1
$ZV: Cache for UNIX (Red Hat Enterprise Linux for x86-64) 2017.1 (Build 792U) Mon Mar 20 2017 19:19:52 EDT
0
0 877
Discussion (9)3
Log in or sign up to continue

If property altura would be of type %Integer, you could use parameter d :
d - output Caché numeric properties that have value "" as null

SET tSC  = ##Class(%ZEN.Auxiliary.jsonProvider).%WriteJSONStreamFromObject(.tSteamJson,pObj,,,,"de")

As string, i don't see any parameters in %WriteJSONStreamFromObject that would return null in stead of "".
 

Personally, I am setting the json myself to have total control of JSON output like


set obj = ##class(...).%OpenId(...)
Set json = {"property1" : (obj.Property1), ... }
If obj.Altura="" Do json.%Set("Altura","","null")
;;%Set allows to set the correct datatype like numeric, boolean,string or force null
Write json.%ToJSON()

Thanks! I initially set it to integer but the client needs the numbers to be sent between quotes, so, of 'string' type.

COS language don't have null type, so null is converted to "". It is possible set a parameter, see: 

parameter %JSONNULL;

If %JSONNULL is true (=1), then unspecified properties are exported as the null value. Otherwise the field corresponding to the property is just skipped during export.

See it: https://community.intersystems.com/post/set-null#comment-138976

Thanks! In my version of Connect that property doesn't work. :(

In IRIS there is a whole load of JSON functionality that can be ing=herited into a class and for the [properties of the class you can specify the JSONNAME, JSONIGNORE, and various others (similar to the XML qualifiers in the Attributes of an XML Enable class) and so it is very easy to specify how to read a JSON message into an Object and visa versa. Likewise you can deal with null, true, false. I have also discovered, while battling with JSON in Ensemble that the value that gets written to a json string depends on where it comes from: for example, if you use SQL to retrieve your data to populate a json string then the LogicalToODBC method of a property will rer=trun null if the internal value is "" but will return "" if you are using objects. In the same way, a boolean field in SQL will display true or false if the display mode is ODBC.  However I agree with Danny that it is best to use the %Set `method to set the correct values into the JSON string for null, true, false. I have been unable to find any other way of doing this in Ensemble.

Nigel

Unfortunately this class (%JSON.Adaptor) is not available in my version of Connect.   triste

Correct:  Your version is: 
 Cache for UNIX (Red Hat Enterprise Linux for x86-64) 2017.1 (Build 792U) Mon Mar 20 2017 19:19:52 EDT
Backport of %JSON classes to Caché is not available

New (2016.2+) Dynamic Objects support NULLs:

set mynull={"def":null}
set mynull2 = {}
do mynull2.%Set("def","","null")