util.JSON.stringify

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

Syntax

util.JSON.stringify(
   source { RECORD | DYNAMIC ARRAY } )
  RETURNING result STRING
  1. source is the program variable to be converted to a JSON string.
  2. result is a JSON formatted string created from the source record.

Usage

The util.JSON.stringify() 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.stringifyOmitNulls(), empty records (where all members are NULL), and empty arrays will be written in the JSON string.

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.

Example

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
Displays following output:
=== 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():
{
}