util.JSON.parse

Parses a JSON string and fills program variables with the values.

Syntax

util.JSON.parse(
   s STRING,
   variableRef { base-type
                 | record-type
                 | dynamic-array-type
                 | dictionary-type
                 }
   )
  1. s is a string value that contains JSON formatted data.
  2. variableRef is the variable to be initialized with values of the JSON string.
    Important: The variableRef parameter is passed by reference to the method.
  3. base-type is a base data type of Genero (INTEGER, DATE, VARCHAR)
  4. record-type is a RECORD ... END RECORD type.
  5. dynamic-array-type is a DYNAMIC ARRAY OF ... type.
  6. dictionary-type is a DICTIONARY OF ... type.

Usage

The util.JSON.parse() class method scans the JSON source string passed as parameter and fills the destination variable members by name.

If the provided string is not valid JSON, the parse() method will raise error -8109. Consider enclosing the parse() method call in a TRY/CATCH block, if the source string can be malformed JSON.

The destination variable is expected to have the same structure as the JSON source data, it can be a RECORD, DYNAMIC ARRAY or a DICTIONARY.

The parse() method initializes the target variable to NULL before the parsing starts.

See JSON support for details on how the destination variable is populated when the structures are not identical.

Example

IMPORT util
MAIN
    DEFINE cust_rec RECORD
               cust_num INTEGER,
               cust_name VARCHAR(30),
               order_ids DYNAMIC ARRAY OF INTEGER
           END RECORD
    DEFINE js STRING
    LET js='{ "cust_num":2735, "cust_name":"McCarlson",
              "order_ids":[234,3456,24656,34561] }'
    TRY
        CALL util.JSON.parse( js, cust_rec )
        DISPLAY cust_rec.cust_name
        DISPLAY cust_rec.order_ids[4]
    CATCH
        DISPLAY "ERROR:", STATUS
    END TRY
END MAIN