Salesforce

How to extract a JSON array into a temp-table

« Go Back

Information

 
TitleHow to extract a JSON array into a temp-table
URL NameHow-to-extract-a-JSON-array-into-a-temp-table
Article Number000185701
EnvironmentProduct: OpenEdge
Version: 11.x
OS: All supported platforms
Question/Problem Description
How to extract a JSON array into a temp-table

Sample code to parse JSON input including an array and store it in an ABL temp-table.
Steps to Reproduce
Clarifying Information
Error Message
Defect Number
Enhancement Number
Cause
Resolution
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.
Workaround
Notes
Note that the format of the JSON input must be known in advance in order to parse it in this way. See the article references below for other examples of parsing JSON input in various formats and storing it into ABL temp-tables.

The attached file 000069712.zip contains the sample code from this article and a sample JSON input file.

References to Other Documentation:

OpenEdge Development: ABL Reference, "Class, Interface and Enumeration Reference"
OpenEdge Development: Working with JSON, Chapter 2, "Parsing and Serializing JSON Objects and Arrays"

Progress Articles:

000040659, How to import a JSON object using ParseFile?
000035585, How to read a JSON object into a temp-table
000062218, How to use the JsonObject class with a temp-table
000069143, How to store JSON from an HTTP service in a temp-table
 
Keyword Phrase
Last Modified Date6/9/2016 3:19 PM

Powered by