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

Reading JSON into a data object with defined schema

The READ-JSON( ) method functions in one of two ways, depending on whether or not its target ABL data object has a defined schema. The most predictable results come when you read JSON data into a data object with a defined schema. In this case, the AVM uses the names to match the ABL ProDataSet, temp-tables, and fields to the JSON objects and name/value pairs. It ignores JSON data for any unmatched data object or field. The AVM raises an error if a matching JSON value does not match the corresponding ABL field's data type.
Note: The examples used in this chapter are available in the doc_samples\src\prodoc\json directory.
The following procedure reads the JSON data that was output by write-json-pds2.p into a ProDataSet that uses a subset of the fields in the original temp-table. It then writes the new ProDataSet out as JSON to another file:
/* read-json-pds2.p */
{pi-json-parameterVarDefs.i} /* parameter variable definitions */

DEFINE TEMP-TABLE ttCustomer NO-UNDO
  FIELD CustNum     LIKE Customer.CustNum
  FIELD Name        LIKE Customer.Name
  FIELD State       LIKE Customer.State.

DEFINE TEMP-TABLE ttOrder    NO-UNDO
  FIELD OrderNum    LIKE Order.Ordernum
  FIELD CustNum     LIKE Order.CustNum
  FIELD ShipDate    LIKE Order.ShipDate.

DEFINE TEMP-TABLE ttInvoice  NO-UNDO
  FIELD Invoicenum  LIKE Invoice.Invoicenum
  FIELD OrderNum    LIKE Invoice.OrderNum
  FIELD InvoiceDate LIKE Invoice.InvoiceDate.

DEFINE DATASET dsOrderLog FOR ttCustomer, ttOrder, ttInvoice
  DATA-RELATION CustOrd FOR ttCustomer,
    ttOrder   RELATION-FIELDS(CustNum,CustNum)   NESTED
  DATA-RELATION OrdInv  FOR ttOrder,
    ttInvoice RELATION-FIELDS(OrderNum,OrderNum) NESTED.

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

hdsOrderLog = DATASET dsOrderLog:HANDLE.

ASSIGN
  cSourceType = "file"
  cFile = "dsOrderLog2.json"
  cReadMode = "EMPTY".

lRetOK = hdsOrderLog:READ-JSON(cSourceType, cFile, cReadMode).

ASSIGN
  cTargetType = "file"
  cFile = "dsOrderLog3.json"
  lFormatted = TRUE
  cEncoding = ?.

lRetOK = hdsOrderLog:WRITE-JSON(cTargetType, cFile, lFormatted).
When you compare the JSON files, you see that only the data that fit into the new ProDataSet made it from dsOrderLog2.json to dsOrderLog3.json.