NULLs and empty structures
Unlike Genero BDL, JSON distinguishes NULL, empty and undefined elements.
JSON notation for NULL, empty and undefined
NULL element is defined with the null
keyword. In the next example, the "name" element is
null:{ "key":345, "name":null }{ "key":345, "address":{} }{ "key":345, "list":[] }{ "key":345 }States of Genero BDL variables
Genero BDL variables defined with a primitive type such as INTEGER can have a
value or can be NULL, but they cannot be undefined as in JSON.
RECORD variables cannot be NULL. However, all elements of a
RECORD can be NULL and thus the record can be considered as empty
or null.
A DYNAMIC ARRAY containing zero elements can be considered as empty or null.
Controlling JSON serialization from BDL
By default, the util.JSON.stringify() method writes all elements of the BDL variable.
DEFINE rec RECORD
field1 INTEGER,
subarr DYNAMIC ARRAY OF INTEGER
END RECORD
INITIALIZE rec.* TO NULL
LET rec.field1 = 999
DISPLAY util.JSON.stringify(rec){"field1":999,"subarr":[]}RECORD or DYNAMIC ARRAY elements
in the JSON string, use the util.JSON.stringifyOmitNulls()
method:IMPORT util
MAIN
DEFINE rec RECORD
field1 INTEGER,
subarr DYNAMIC ARRAY OF INTEGER
END RECORD
INITIALIZE rec.* TO NULL
LET rec.field1 = 999
DISPLAY util.JSON.stringifyOmitNulls(rec)
END MAINsubarr element is omitted because it is
empty):{"field1":999}If you want to have a fine-grained control on the JSON serialization of null values and empty
records and dynamic arrays, use the util.JSON.stringify() method, in conjunction
with the json_null attribute when defining the BDL variable. The
json_null variable definition attribute can be set to "null" or
"undefined".
Using the json_null="null" attribute
json_null="null" attribute, the following
BDL elements will be represented with the null keyword in the resulting JSON
string:- Simple primitive variables that are
NULL, RECORDvariables where all members areNULL,- Empty
DYNAMIC ARRAYthat contain zero elements.
DEFINE rec RECORD
field1 INTEGER ATTRIBUTES(json_null="null"),
field2 CHAR(1) ATTRIBUTES(json_null="null"),
subrec1 RECORD ATTRIBUTES(json_null="null")
field11 INTEGER,
fiedl12 VARCHAR(30)
END RECORD,
subarr1 DYNAMIC ARRAY ATTRIBUTES(json_null="null") OF INTEGER
END RECORD
INITIALIZE rec.* TO NULL
DISPLAY util.JSON.stringify(rec){"field1":null,"field2":null,"subrec1":null,"subarr1":null}Using the json_null="undefined" attribute
json_null="undefined" attribute, the
following BDL elements will be omitted in the resulting JSON string:- Simple primitive variables that are
NULL(in fact,json_null="undefined"is the default for primitives), RECORDvariables where all members areNULL,- Empty
DYNAMIC ARRAYthat contain zero elements.
DEFINE rec RECORD
field1 INTEGER ATTRIBUTES(json_null="undefined"),
field2 CHAR(1) ATTRIBUTES(json_null="undefined"),
subrec1 RECORD ATTRIBUTES(json_null="undefined")
field11 INTEGER,
fiedl12 VARCHAR(30)
END RECORD,
subarr1 DYNAMIC ARRAY ATTRIBUTES(json_null="undefined") OF INTEGER
END RECORD
INITIALIZE rec.* TO NULL
LET rec.field1 = 999
DISPLAY util.JSON.stringify(rec){"field1":999}Exception of NULL dynamic array elements
If elements of a dynamic array
are NULL, the JSON serialization class will always produce a null
keyword, even if you specify the json_null="undefined" attribute at the primitive
type level.
DEFINE rec RECORD
subarr DYNAMIC ARRAY OF INTEGER ATTRIBUTES(json_null="undefined")
END RECORD
INITIALIZE rec.* TO NULL
LET rec.subarr[3] = 999
DISPLAY util.JSON.stringify(rec) {"subarr":[null,null,999]}