util.JSON.parse
Parses a JSON string and fills program variables with the values.
Syntax
util.JSON.parse(
s STRING,
variableRef { primitive-type
| record-type
| array-type
| dictionary-type
}
)
- s is a string value that contains JSON formatted data.
- variableRef is the variable to be initialized with values of the JSON string.
Important: The variableRef parameter is passed by reference to the method.
- primitive-type is a primitive data type of Genero (
INTEGER
,DATE
,VARCHAR
) - record-type is a
RECORD ... END RECORD
type. - array-type is a
DYNAMIC ARRAY OF ...
orARRAY[n] OF ...
type. - 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