util.JSON.stringifyOmitNulls

Transforms a record variable to a flat JSON formatted string, by excluding empty records and empty arrays.

Syntax

util.JSON.stringifyOmitNulls(
   value { RECORD | DYNAMIC ARRAY } )
  RETURNS STRING
  1. value is the program variable to be converted to a JSON string.

Usage

The util.JSON.stringifyOmitNulls() class method takes a RECORD or DYNAMIC ARRAY variable as parameter, and generates the corresponding data string in JSON format, as defined in the [RFC4627] specification.

Important: Unlike util.JSON.stringify(), empty records (where all members are NULL), and empty arrays will NOT be written in the JSON string. For detailed control on null and empty variables when serializing to JSON elements, use the json_null variable definition attribute and the stringify() method. The method stringifyOmitNulls() behaves like stringify(), when all variable elements are defined with the json_null="undefined" attribute.

The method raises error -8110 if the JSON string cannot be generated.

For more details about FGL to JSON conversion, see JSON support.

Example

IMPORT util
MAIN
    DEFINE rec RECORD
             field1 INTEGER,
             field2 CHAR(1),
             subrec1 RECORD
               field11 INTEGER,
               fiedl12 VARCHAR(30)
             END RECORD,
             subarr1 DYNAMIC ARRAY OF INTEGER
           END RECORD
    INITIALIZE rec.* TO NULL
    LET rec.field1 = 999
    DISPLAY "stringify()         : ", util.JSON.stringify(rec)
    DISPLAY "stringifyOmitNulls(): ", util.JSON.stringifyOmitNulls(rec)
END MAIN
Output:
stringify()         : {"field1":999,"subrec1":{},"subarr1":[]}
stringifyOmitNulls(): {"field1":999}