WRITE-JSON( ) method
- Last Updated: February 11, 2026
- 7 minute read
- OpenEdge
- Version 13.0
- Documentation
Writes a specified JSON string, Progress.Json.ObjectModel.JsonArray object, or Progress.Json.ObjectModel.JsonObject object from a corresponding ProDataSet,
temp-table, or temp-table buffer object. In the case of a temp-table buffer object, all the
records of the temp-table associated with the buffer object are written to the JSON string
or JsonArray object—not just the one record in the
buffer.
Return type: LOGICAL
Applies to: Buffer object handle, ProDataSet object handle, Temp-table object handle
Syntax
|
- target-type
- A CHARACTER expression that specifies the target type for the JSON
output. Valid values are
"FILE","STREAM","STREAM-HANDLE","MEMPTR","LONGCHAR","JsonArray", and"JsonObject". - file
- A CHARACTER expression that specifies the name of a file to which the AVM writes the JSON string. You can specify an absolute pathname or a pathname relative to the current working directory. If a file with the specified name already exists, the AVM verifies that the file is writeable and overwrites the file.
- stream
- A CHARACTER expression that specifies the name of a stream. If you
specify the empty string (
""), the AVM writes the JSON string to the default unnamed output stream. For WebSpeed, write the JSON string to the WebSpeed-defined output stream (WEBSTREAM).For more information about using ABL unnamed output streams, see the
DEFINE STREAMstatement reference entry and the topics on alternate I/O sources in OpenEdge Programming Interfaces. - stream-handle
- A HANDLE variable that specifies a stream object handle.
- memptr
- A MEMPTR variable to contain the JSON string in memory. If you do not
specify the encoding parameter, the AVM encodes the text written to the MEMPTR as UTF-8.
This method allocates the required amount of memory for the JSON string and sets the
size of the variable. When you are finished using the MEMPTR, you must free the
associated memory, by executing
SET-SIZE(memptr) = 0on the MEMPTR. - longchar
- A LONGCHAR variable to contain the JSON string in memory.
The AVM saves the JSON string to the LONGCHAR variable in the code page that corresponds to the character encoding you specify in the encoding option. If you do not specify a character encoding for the JSON string, the AVM saves the LONGCHAR variable in UTF-8.
If the LONGCHAR variable's code page is fixed (that is, set using the
FIX-CODEPAGEstatement) and the fixed code page is not equivalent to the character encoding you specify in the encoding option, theWRITE-JSON( )method generates an error message and returns FALSE. The JSON string is not saved to the LONGCHAR. - JsonArray
- A reference to a
Progress.Json.ObjectModel.JsonArrayobject to which the AVM writes the contents of the associated temp-table. Any prior contents of the specified JsonArray are removed. - JsonObject
- A reference to a
Progress.Json.ObjectModel.JsonObjectobject to which the AVM writes the contents of the associated ProDataSet or temp-table. Any prior contents of the specified JsonObject are removed. - formatted
- An optional LOGICAL expression where TRUE directs the AVM to format
the JSON string in a hierarchical manner using extra white space, carriage returns, and
line feeds. The default value is FALSE.
If you specify the Unknown value (
?), the method uses the default value of FALSE.The formatted option is ignored for JsonArray and JsonObject targets.
- encoding
- An optional CHARACTER expression that specifies the name of the
character encoding the AVM uses to write the JSON output. The default encoding is
"UTF-8".
The encoding name must specify a Unicode transformation format. Valid values are
"UTF-8","UTF-16","UTF-16BE","UTF-16LE","UTF-32","UTF-32BE", and"UTF-32LE".Note: If you specify the empty string ("") or the Unknown value (?), the AVM uses the default encoding of UTF-8. - omit-initial-values
- An optional LOGICAL expression where TRUE directs the AVM to exclude
temp-table fields containing their initial values from the JSON output, and FALSE
directs the AVM to include all temp-table field data in the output. The default value is
FALSE. If you specify the Unknown value (
?), the method uses the default value of FALSE.When working with large ProDataSets, omitting fields containing their initial values can yield smaller JSON values, more efficient network transfers, and performance gains with the
READ-JSON( )andWRITE-JSON( )methods.This behavior applies both to temp-table fields that have the default initial value for its data type, and for fields that have an initial value set with the ABL
INITIALoption.Although using the omit-initial-values option can give your application performance and resource use improvements, you must be sure that the consumers of the generated JSON output can correctly handle it. The ABL
READ-JSON( )method always populates created records with initial values from the temp-table or ProDataSet definition. Other applications might not do this. - omit-outer-object
- An optional LOGICAL expression that indicates whether the outer-most
object is included in the JSON output. TRUE directs the AVM to remove the outer-most
object on output. FALSE directs the AVM to include the outer-most object in the
output.
For a JSON string or JsonObject target, the default value is FALSE. If you specify the Unknown value (
?), the method also uses the default value of FALSE. When writing a temp-table to a JsonObject target, omit-outer-object must be FALSE. If you specify TRUE, theWRITE-JSON( )method generates an error message and returns FALSE.For a JsonArray target, the default value for omit-outer-object is TRUE. If you specify the Unknown value (
?), the method also uses the default value of TRUE. When writing a temp-table or temp-table buffer object to a JsonArray target, omit-outer-object must be TRUE. If you specify FALSE, theWRITE-JSON( )method generates an error message and returns FALSE. - write-before-image
- An optional LOGICAL expression where TRUE directs the AVM to include
ProDataSet before-image data and error information in the JSON output. The default value
is FALSE.
This element can only be set to TRUE for a ProDataSet. If its value is set to TRUE for a temp-table or buffer handle, the
WRITE-JSON( )method generates an error message and returns a value of FALSE.
Notes
- When writing data from a ProDataSet object that contains data-relations, you
can nest child rows of a ProDataSet buffer within their parent rows in the resulting JSON
output by:
- Setting the
NESTEDattribute on the data-relation object handle to TRUE - Specifying the
NESTEDoption for the data-relation on the DEFINE DATASET statement - Specifying the
NESTEDoption in the ADD-RELATION( ) method - Specify a
PARENT-ID-RELATIONin the DEFINE DATASET statement - Using the ADD-PARENT-ID-RELATION( ) method on a dynamic ProDataSet object
- Setting the
- You cannot call
WRITE-JSON( )on a database buffer handle. WRITE-JSON( )can serialize fields with data types based on class-based objects. The following restrictions apply to any class-based objects that are to be written to the JSON target:- The object's class and all of the classes in its hierarchy must be
marked as
SERIALIZABLE. See the CLASS statement entry for more information. - All of the object's data members that are themselves defined as
class-based objects must be of class types that are also marked
SERIALIZABLE. ABL enums are also allowed because they are implicitly serializable. - By default, all non-static, public data members and read-write
properties are serialized. You can include protected and private data members,
read-write properties, temp tables, and ProDataSets in serialization by defining them
using the
SERIALIZABLEoption. See theDEFINEentry for each element for more details. - To be able to deserialize an object, the API for the object's class at the time of serialization must be compatible with the API for the class at the time of deserialization. At a minimum, the data members contained in the JSON target for that object should exist in the class, be serializable, and have compatible data types.
- The content of static data members is not serialized, and the state of queries, buffers, open files, streams, and event subscriptions, for example, are not maintained.
- The object's class and all of the classes in its hierarchy must be
marked as
- When executing the method, there is no guarantee as to the contents of the buffers involved after the method finishes executing.
- Binary types, including BLOB, RAW, and ROWID are base64-encoded/decoded. See Data type mapping in Use JSON with ABL Applications for more information.
Examples
The following code example defines a static ProDataSet object, attaches its data sources, fills the ProDataSet object, and writes the ProDataSet object to a JSON string in a nested manner:
|
The following code example defines a static temp-table object, populates the temp-table object (code not shown), and writes the temp-table object to a JSON string:
|
The following code example defines and populates a static temp-table
object with different assigned, initial, and default value settings. It also defines and
instantiates both a JsonArray and JsonObject object, then writes the JSON representation of the temp-table to both
the JsonArray and the JsonObject. As it writes out the JSON, it generates MESSAGE
statement output to a TtabWriteJsonArrObj.out file to
show the results by returning and displaying both the written JSON array and object contents
as a string.
|
The following shows the MESSAGE statement output
generated in TtabWriteJsonArrObj.out.
|
See also
ENCODING attribute, FIX-CODEPAGE statement, FOREIGN-KEY-HIDDEN attribute, NESTED attribute, Progress.Json.ObjectModel.JsonArray class, Progress.Json.ObjectModel.JsonObject class, READ-JSON( ) method, SERIALIZE-ROW( ) method