| The util.JSON class / util.JSON methods | |
Transforms a record variable to a flat JSON formatted string, by excluding empty records and empty arrays.
util.JSON.stringifyOmitNulls( source { RECORD | DYNAMIC ARRAY } ) RETURNING result STRING
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.
For more details about FGL to JSON conversion, see Genero BDL to JSON conversion rules.
The method raises error -8110 if the JSON string cannot be generated.
IMPORT util
MAIN
    DEFINE cust_rec RECORD
             cust_data RECORD
               cust_num INTEGER,
               cust_name VARCHAR(30),
               cust_phone VARCHAR(20)
             END RECORD,
             order_ids DYNAMIC ARRAY OF INTEGER
           END RECORD
    DEFINE js STRING
    LET cust_rec.cust_data.cust_num = 345
    LET cust_rec.cust_data.cust_name = "McMaclum"
    LET cust_rec.cust_data.cust_name = NULL
    LET cust_rec.order_ids[1] = 4732
    LET cust_rec.order_ids[2] = NULL
    LET cust_rec.order_ids[3] = 2194
    DISPLAY "=== With values:"
    LET js = util.JSON.stringify( cust_rec )
    DISPLAY "1: stringify():\n", util.JSON.format( js )
    LET js = util.JSON.stringifyOmitNulls( cust_rec )
    DISPLAY "2: stringifyOmitNulls():\n", util.JSON.format( js )
    DISPLAY "=== NULLs:"
    INITIALIZE cust_rec TO NULL
    LET js = util.JSON.stringify( cust_rec )
    DISPLAY "3: stringify():\n", util.JSON.format( js )
    LET js = util.JSON.stringifyOmitNulls( cust_rec )
    DISPLAY "4: stringifyOmitNulls():\n", util.JSON.format( js )
END MAIN
=== With values:
1: stringify():
{
    "cust_data": {
        "cust_num": 345
    },
    "order_ids": [4732,null,2194
    ]
}
2: stringifyOmitNulls():
{
    "cust_data": {
        "cust_num": 345
    },
    "order_ids": [4732,null,2194
    ]
}
=== NULLs:
3: stringify():
{
    "cust_data": {
    },
    "order_ids": [
    ]
}
4: stringifyOmitNulls():
{
}