Salesforce

How to read a JSON object into a temp-table

« Go Back

Information

 
TitleHow to read a JSON object into a temp-table
URL Name000035585
Article Number000159454
EnvironmentProduct: OpenEdge
Version: 10.2x, 11.x, 12.x
OS: All Supported Platforms
Question/Problem Description
How to read a JSON object into a temp-table

How to read a JSON object serialized from a temp-table into another temp-table, as part of a round-trip application.

How to store JSON received in a similar format as that produced by the WRITE-JSON() method

How to infer a temp-table schema from the structure of JSON input
Steps to Reproduce
Clarifying Information
Error Message
Defect Number
Enhancement Number
Cause
Resolution
The following sample code (taken from the online help) shows how to read a JSON object into a temp-table, inferring the structure of the temp-table from the structure of the JSON.

Note that this method only works if the format of the JSON is similar to that which would be produced by the WRITE-JSON() method of the temp-table handle in a round-trip application. Specifically, READ-JSON() expects any nested child records to be represented as an Array.
DEFINE VARIABLE cSourceType AS CHARACTER NO-UNDO.
DEFINE VARIABLE cReadMode   AS CHARACTER NO-UNDO.
DEFINE VARIABLE cFile       AS CHARACTER NO-UNDO.
DEFINE VARIABLE lRetOK      AS LOGICAL   NO-UNDO.
DEFINE VARIABLE httCust     AS HANDLE    NO-UNDO.

CREATE TEMP-TABLE httCust.
    
ASSIGN  cSourceType = "file"  
        cFile       = "ttcust.json"   
        cReadMode   = "empty".
 
lRetOK = httCust:READ-JSON(cSourceType, cFile, cReadMode).

/* Sample Code to Read Data Loaded into the Temp-Table */

DEFINE VARIABLE hQuery      AS HANDLE    NO-UNDO.
DEFINE VARIABLE hBuffer     AS HANDLE    NO-UNDO.
DEFINE VARIABLE iNumFields  AS INTEGER   NO-UNDO.
DEFINE VARIABLE iLoop       AS INTEGER   NO-UNDO.

ASSIGN hBuffer    =  httCust:DEFAULT-BUFFER-HANDLE
       iNumFields = hBuffer:NUM-FIELDS.

CREATE QUERY hQuery.

hQuery:SET-BUFFERS(httCust:DEFAULT-BUFFER-HANDLE).
hQuery:QUERY-PREPARE("FOR EACH " + httCust:NAME).
hQuery:QUERY-OPEN().
hQuery:GET-FIRST().

DO WHILE hQuery:QUERY-OFF-END = FALSE:
    DO iLoop = 1 TO iNumFields:
        // we use the STRING() function and BUFFER-VALUE attribute
        // below instead of the STRING-VALUE attribute so that the
        // data value isn't truncated to X(8) format. Thanks to the
        // customer who noticed the truncation behavior.
        MESSAGE STRING(hBuffer:BUFFER-FIELD(iLoop):BUFFER-VALUE,"X(80)" VIEW-AS ALERT-BOX.
    END.
    hQuery:GET-NEXT().
END.

hQuery:QUERY-CLOSE().

DELETE OBJECT hQuery.
DELETE OBJECT hBuffer.
Workaround
Notes
The ttcust.json file referenced in the code is attached, however, it can also be found in the <YourInstallDirectory>\src\prodoc\json directory.

References to Other Documentation:

OpenEdge Development: Working with JSON, Reading and Serializing JSON to/from ProDataSets and Temp-tables:
https://documentation.progress.com/output/ua/OpenEdge_latest/index.html#page/dvjsn/reading-and-serializing-json-to-2ffrom-prodatasets.html

Progress Articles:
 Does JSON serialization of a ProDataSet support representing a single nested child record as a simple string instead of an array ?
 How to store JSON from an HTTP service in a temp-table
 
Keyword Phrase
Last Modified Date2/25/2022 7:53 PM

Powered by