Try OpenEdge Now
skip to main content
Working with JSON
Reading and Serializing JSON to/from ProDataSets and Temp-tables : Writing JSON from a temp-table, temp-table buffer, or a ProDataSet : Writing JSON from a temp-table
 

Writing JSON from a temp-table

The following code example defines a static temp-table, fills the temp-table object, and writes the temp-table object to a JSON string:
/* write-json-tt.p */
{pi-json-parameterVarDefs.i} /* parameter variable definitions */
{pi-write-json-tt.i}         /* ttCust definition */

DEFINE VARIABLE httCust      AS HANDLE  NO-UNDO.
DEFINE VARIABLE lReturnValue AS LOGICAL NO-UNDO.

httCust = TEMP-TABLE ttCust:HANDLE.

FOR EACH Customer WHERE CustNum < 4:
  CREATE ttCust.
  BUFFER-COPY Customer TO ttCust.
END.

ASSIGN
  cTargetType = "FILE"
  cFile       = "ttCust.json"
  lFormatted  = TRUE
  cEncoding   = ?.

lReturnValue = httCust:WRITE-JSON(cTargetType, cFile, lFormatted, cEncoding).
The following is an excerpt of the JSON produced by this procedure:
{"ttCust": [
  {
    "CustNum": 1,
    "Country": "USA",
    "Name": "Lift Tours",
    "Address": "276 North Drive",
    "Address2": "",
    "City": "Burlington",
    "State": "MA",
    "PostalCode": "01730",
    "Contact": "Gloria Shepley",
    "Phone": "(617) 450-0086",
    "SalesRep": "HXM",
    "CreditLimit": 66700.00,
    "Balance": 903.64,
    "Terms": "Net30",
    "Discount": 35,
    "Comments": "This customer is on credit hold.",
    "Fax": "",
    "EmailAddress": ""
  },
  {
    "CustNum": 2,
    ...  },
  {
    "CustNum": 3,
    ...  }
]}
* Writing JSON from a ProDataSet with before-image data