Salesforce

Sample ABL code to generate JSON output for multiple tables without using an array (dataset)

« Go Back

Information

 
TitleSample ABL code to generate JSON output for multiple tables without using an array (dataset)
URL NameSample-ABL-code-to-generate-JSON-output-for-multiple-tables-without-using-an-array-dataset
Article Number000177162
EnvironmentProduct: OpenEdge
Version: 11.x, 12.x
OS: All Supported Platforms
Other: JSON
Question/Problem Description
Some interfaces that require JSON input strings may have requirements to receive a JSON file containing multiple table structures.  This can easily be generated in the ABL using the WRITE-JSON method on a DATASET object, however the dataset uses a JSON array to construct such output, which includes the dataset as the top level complex data structure, and each record in the contained tables is an array.

The array structure is necessary when writing out multiple table records because each instance of a field in a table is a unique key, which will result in a runtime error.  
For interfaces that are expecting a single record in each table, without using an array, the below example can be used.
 
Steps to Reproduce
Clarifying Information
Error Message
Defect Number
Enhancement Number
Cause
Resolution
The below code uses the Progress.Json.ObjectModel.JsonObject class to generate the JSON output:
 
USING Progress.Json.ObjectModel.JsonObject.

DEFINE TEMP-TABLE invoice
    FIELD internalid AS CHARACTER.

DEFINE TEMP-TABLE notes
    FIELD invoice_note         AS CHARACTER
    FIELD apply_invoice_note   AS CHARACTER
    FIELD apply_reference      AS CHARACTER.

DEFINE VARIABLE oJson        AS JsonObject NO-UNDO. 
DEFINE VARIABLE oJsonInvoice AS JsonObject NO-UNDO. 
DEFINE VARIABLE oJsonNotes   AS JsonObject NO-UNDO. 

DEFINE VARIABLE jSon_string  AS CHARACTER  NO-UNDO.

/* Create a record in each of the temp-tables */
CREATE invoice.
invoice.internalid = "5071517".

CREATE notes.
ASSIGN notes.invoice_note       = "note from PROGRESSS"
       notes.apply_invoice_note = "apply note"
       notes.apply_reference    = "apply reference".

/* Create new JsonObjects */
oJson        = NEW JsonObject().
oJsonInvoice = NEW JsonObject().
oJsonNotes   = NEW JsonObject().

/* Add the Invoice table */
oJson:Add("invoice", oJsonInvoice).
oJsonInvoice:READ(TEMP-TABLE invoice:HANDLE).

/* Add the Notes table */
oJson:Add("notes", oJsonNotes).
oJsonNotes:READ(TEMP-TABLE notes:HANDLE).

/* Write the JSON string to a character variable */
oJson:Write(jSon_string,TRUE). 

MESSAGE STRING(jSon_string) VIEW-AS ALERT-BOX.

 
Workaround
Notes
using JsonArray instead JsonObject for the temp-tables:
 
USING Progress.Json.ObjectModel.JsonObject.
USING Progress.Json.ObjectModel.JsonArray . 
DEFINE VARIABLE oJson AS JsonObject NO-UNDO.
/* Here JsonArray instead */
DEFINE VARIABLE aJson1stTable AS JsonArray NO-UNDO.
DEFINE VARIABLE aJson2ndTable AS JsonArray NO-UNDO.



DEFINE TEMP-TABLE ttTable1
FIELD Field1 AS CHAR
FIELD Field2 AS CHAR.

DEFINE TEMP-TABLE ttTable2
FIELD Field21 AS CHAR
FIELD Field22 AS CHAR.


/* Create new JsonObjects */
oJson = NEW JsonObject().
/* Create JsonArrays */
aJson1stTable = NEW JsonArray().
aJson2ndTable = NEW JsonArray().



CREATE ttTable1.
ASSIGN ttTable1.Field1 = "Field # 1"
ttTable1.Field2 = "Field # 2".
CREATE ttTable1.
ASSIGN ttTable1.Field1 = "Field # 1 - 1"
ttTable1.Field2 = "Field # 1 - 2".

CREATE ttTable2.
ASSIGN ttTable2.Field21 = "Field # 2 1"
ttTable2.Field22 = "Field # 2 2".


oJson:Add("ttTable1", aJson1stTable).
aJson1stTable:READ(TEMP-TABLE ttTable1:HANDLE).

oJson:Add("ttTable2", aJson2ndTable).
aJson2ndTable:READ(TEMP-TABLE ttTable2:HANDLE).

DEF STREAM st1.

OUTPUT STREAM st1 TO "json.txt".
oJson:writeStream("ST1",YES).
OUTPUT STREAM st1 CLOSE.

 
Keyword Phrase
Last Modified Date5/25/2023 7:29 PM

Powered by