Example 2: Handling arrays with null values

This example shows how null values in arrays are handled during serialization.

The following program reads data with null values into the array arr, converts the array to JSON, and writes the result to standard output. The conversion calls are enclosed in a TRY/CATCH block to detect JSON serialization errors.

 # ... 
 DEFINE t TEXT
 DEFINE writer json.JSONWriter
 DEFINE arr DYNAMIC ARRAY OF STRING
    LOCATE t IN MEMORY
    INITIALIZE t to NULL

    LET arr[1] = "foo"
    LET arr[2] = NULL
    LET arr[3] = NULL

    TRY 
        LET writer = json.JSONWriter.Create()
        CALL writer.setOutputCharset("UTF-8")
        CALL writer.writeToText(t)
        CALL writer.startJSON()
        CALL json.Serializer.variableToJSON(arr, writer)
        CALL writer.endJSON()
        CALL writer.close()
    CATCH
        DISPLAY "\narr with NULL value:" || "ERROR " || status || " " || sqlca.sqlerrm
    END TRY
    # ...

When this array is serialized, error-15807 is raised:

arr with NULL value:ERROR -15807 Array cannot serialize 'null' elements. It requires json_null="null"
An array element can be serialized as NULL only if it is explicitly enabled on the element type:
DEFINE arr DYNAMIC ARRAY OF STRING ATTRIBUTE(json_null="null")
Alternatively, you can allow null values globally by setting the allowNullAsDefault option:
CALL json.Serializer.setOption( "allowNullAsDefault", 1 )
If the entire array is NULL, no error is raised. For example:
INITIALIZE arr TO NULL
In this case, GWS serializes the array as an empty object ({}), or as null if the json_null="null" attribute is applied to the array:
DEFINE arr DYNAMIC ARRAY ATTRIBUTE(json_null="null") OF STRING ATTRIBUTE(json_null="null")

For more details, refer to the OpenAPI documentation.