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 ProDataSet with before-image data
 
Writing JSON from a ProDataSet with before-image data
The following code example defines and fills a static ProDataSet object, turns on tracking, and makes a change to the ProDataSet. The code then writes the ProDataSet object to a JSON string and includes the before-image data:
/* write-json-pds3.p */
{pi-json-parameterVarDefs.i} /* parameter variable definitions */
{pi-write-json-pds1.i} /* dsOrderLog definition - no nesting */

DEFINE VARIABLE hdsOrderLog AS HANDLE NO-UNDO.
DEFINE VARIABLE lRetOK AS LOGICAL NO-UNDO.

hdsOrderLog = DATASET dsOrderLog:HANDLE.
DATA-SOURCE dsCustomer:FILL-WHERE-STRING = "WHERE Customer.CustNum = 2 ".
DATASET dsOrderLog:FILL().

/* set tracking-changes so that before-table records are created */
TEMP-TABLE ttCustomer:TRACKING-CHANGES = TRUE.
TEMP-TABLE ttCustomer:TRACKING-CHANGES = TRUE.

/* modify ttCustomer 2 */
FIND ttCustomer WHERE ttCustomer.CustNum = 2.
ASSIGN ttCustomer.EmailAddress = "www.progress.com".

/* set error string on ttCustomer 2 */
BUFFER ttCustomer:ERROR = TRUE.
BUFFER ttCustomer:ERROR-STRING = "ttCustomer 2 error".

/* delete an order */
FIND ttOrder WHERE ttOrder.OrderNum = 94.
DELETE ttOrder.

TEMP-TABLE ttCustomer:TRACKING-CHANGES = FALSE.
TEMP-TABLE ttCustomer:TRACKING-CHANGES = FALSE.

/* write-json with before-image data to file */
ASSIGN
cTargetType = "file"
cFile = "dsOrderLogWithBefore.json"
lFormatted = TRUE
cEncoding = ?
lOmitInitialValues = FALSE
lOmitOuterObject = FALSE
lWriteBeforeImage = TRUE.

lRetOK = hdsOrderLog:WRITE-JSON(cTargetType, cFile, lFormatted,
                                cEncoding, lOmitInitialValues,
                                lOmitOuterObject, lWriteBeforeImage).
The following is an excerpt of the JSON produced by this procedure:
{"dsOrderLog": {
  "prods:hasChanges": true,
  "ttCustomer": [
   {
    "prods:id": "ttCustomer14592",
      "prods:rowState": "modified",
      "prods:hasErrors": true,
      "CustNum": 2,
      ...
      "EmailAddress": "www.progress.com"
    }
  ],
  "ttOrder": [
    {
      "Ordernum": 125,
      ...
    },
    ...
    {
      "Ordernum": 6070,
      ...
  ],
  "ttInvoice": [
    {
      "Invoicenum": 94,
      ...
    },
    {
      "Invoicenum": 124,
      ...
    }
  ],
  "prods:before": {
    "ttCustomer": [
      {
        "prods:id": "ttCustomer14592",
        "prods:rowState": "modified",
        "CustNum": 2,
        ...
        "EmailAddress": ""
      }
    ],
    "ttOrder": [
      {
        "prods:id": "ttOrder12545",
        "prods:rowState": "deleted",
        "CustNum": 2,
        "OrderNum": 94,
        ...
      }
    ]
  },
  "prods:errors": {
    "ttCustomer": [
      {
        "prods:id": "ttCustomer14592",
        "prods:error": "ttCustomer 2 error"
      }
    ]
  }
}}
The prods:before object contains the before-image data for the ProDataSet.