util.JSON.stringify
Transforms a record variable to a flat JSON formatted string, by including empty records and empty arrays.
Syntax
util.JSON.stringify(
value { primitive-type
| record-type
| array-type
| dictionary-type
}
)
RETURNS STRING
- value is the program variable to be converted to a JSON string.
- primitive-type is a primitive data type of Genero (
INTEGER
,DATE
,VARCHAR
) - record-type is a
RECORD ... END RECORD
type. - array-type is a
DYNAMIC ARRAY OF ...
orARRAY[n] OF ...
type. - dictionary-type is a
DICTIONARY OF ...
type.
Usage
The util.JSON.stringify()
class method takes a 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 detailed control on
null and empty variables when serializing to JSON elements, use the json_null
variable definition 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
LET rec.subarr1[3] = 888
DISPLAY "stringify() : ", util.JSON.stringify(rec)
END MAIN
Output:
stringify() : {"field1":999,"subrec1":{},"subarr1":[null,null,888]}