The following example code parses a text file containing a JSON array and stores the array data in an ABL temp-table.
USING Progress.Json.ObjectModel.*.
DEFINE VARIABLE myParser AS ObjectModelParser NO-UNDO.
DEFINE VARIABLE myJsonObj AS JsonObject NO-UNDO.
DEFINE VARIABLE results-array AS JsonArray NO-UNDO.
DEFINE VARIABLE myResultObj AS JsonObject NO-UNDO.
DEFINE VARIABLE iPage AS INTEGER NO-UNDO.
DEFINE VARIABLE iLimit AS INTEGER NO-UNDO.
DEFINE VARIABLE iCount AS INTEGER NO-UNDO.
DEFINE VARIABLE iLength AS INTEGER NO-UNDO.
DEFINE NEW SHARED TEMP-TABLE ttCustomer no-undo
field tt_name as char format "X(30)"
field tt_cust# as char
field tt_address as char format "X(30)"
field tt_address2 as char format "X(30)"
field tt_city as char format "X(20)"
field tt_state as char format "X(2)"
field tt_zip as char format "X(9)"
field tt_country as char format "X(3)"
field tt_phone as char format "X(10)"
field tt_result as int.
/* Read JSON from a text file for this example, to simulate the
more likely real-life scenario of an HTTP response
received from a Web service. */
myParser = NEW ObjectModelParser().
myJsonObj = CAST(myParser:ParseFile("TwoCustomers.json"), JsonObject).
/* Load the results array into a separate JsonArray object */
results-array = myJsonObj:GetJsonArray("results").
/* Get the number of elements in the array */
iLength = results-array:LENGTH.
DO iCount = 1 TO iLength:
/* Each element is an object, so store the current element in a second
JsonObject variable for parsing */
myResultObj = results-array:GetJsonObject(iCount).
/* Store each element of the array element object into a
temp-table record, using the appropriate Get<type> method
on the array element object to get the value. */
CREATE ttCustomer.
ASSIGN
ttCustomer.tt_name = myResultObj:GetCharacter("name")
ttCustomer.tt_address = myResultObj:GetCharacter("address")
ttCustomer.tt_address2 = myResultObj:GetCharacter("address2")
ttCustomer.tt_city = myResultObj:GetCharacter("city")
ttCustomer.tt_state = myResultObj:GetCharacter("state")
ttCustomer.tt_zip = myResultObj:GetCharacter("zip")
ttCustomer.tt_country = myResultObj:GetCharacter("country")
ttCustomer.tt_phone = myResultObj:GetCharacter("phone")
.
END.
/* Delete all objects created by this procedure to avoid memory leaks */
DELETE OBJECT myResultObj NO-ERROR.
DELETE OBJECT results-array NO-ERROR.
DELETE OBJECT myJsonObj NO-ERROR.
DELETE OBJECT myParser NO-ERROR.
/* For this example, display the data extracted from the JSON array */
FOR EACH ttCustomer:
DISPLAY SKIP(2).
DISPLAY ttCustomer WITH 1 COLUMN SIDE-LABELS.
END.