JSON_TO_PROCESS

The JSON_TO_PROCESS script function is used store JSON values in an internal list (process) for further processing (example, iteration over elements).

Syntax

JSON_TO_PROCESS (JSON, JSONPath [,MODE])

Parameters

  • JSON
    A string or variable containing a valid JSON document

  • JSONPath
    A valid JSONPath expression that identifies the values to be stored

  • MODE
    VALUE (default), PATH, or KEYS
    Extracts the value matched by the JSONPath expression (VALUE), the absolute JSONPath to that value (PATH) or the keys of a JSON Object (KEYS).

    Tip: When setting MODE to PATH, the function does not store the matched function value, but its absolute JSONPath. This path can be used to modify the value later (see examples below).

Notes:

  • Use this function when you need to access an array of JSON values, as JSON_GET_VALUE can only access individual (primitive) values
  • The data sequence creates a line for each selected value

Return value

The script function stores values from a JSON document matched by a JSONPath expression in a data sequence (internal list). The reference to the data sequence that is created is returned as a return code.

Runtime errors

JSON_TO_PROCESS will fail with a runtime error on the following conditions:

  • 45334

    If your JSON document is invalid
  • 45342
    Syntax Error in JSONPath expression
  • 45347
    Invalid Mode Parameter: Value, Path or Keys expected
  • 45350
    In keys mode, the JSONPath has to select an object

Examples:

This example uses the script function to print the host IP addresses contained in the JSON structure.

:SET &JSON_DOC# = '[{"host":"192.168.0.10","active":true},{"host":"192.168.0.20","active":false},{"host":"192.168.0.29","active":true}]'
:SET &HND# = JSON_TO_PROCESS(&JSON_DOC#, '$..host' )
:PROCESS &HND#
:SET &HOST_IP# = GET_PROCESS_LINE(&HND#)
:PRINT "&HOST_IP#"
:ENDPROCESS

Activation report result:

U00020408 192.168.0.10
U00020408 192.168.0.20
U00020408 192.168.0.29

Iterate over all keys and values of a JSON object using the KEYS mode:

:SET &JSON_DOC# = '{"serverConfig": {"OS":"Linux","Cores":8,"RAM": 16, "DiskType":"SSD","DiskSpace":512}}'
:SET &OBJPATH# = '$.serverConfig'

! First, we retrieve all keys of the serverConfig object using the "KEYS" mode, i.e. "OS", "Cores",...

:SET &KEYS# = JSON_TO_PROCESS(&JSON_DOC#,&OBJPATH# ,"KEYS")
:PROCESS &KEYS#
:SET &KEY# = GET_PROCESS_LINE(&KEYS#)

! Now, we retrieve the value for that key by concatenating the objectpath and the keyname

:SET &VALUE# = JSON_GET_VALUE(&JSON_DOC#,'&OBJPATH#.&KEY#')
:P '&KEY#:&VALUE#'
:ENDPROCESS

Activation report result:

U00020408 OS:Linux
U00020408 Cores:8
U00020408 RAM:16
U00020408 DiskType:SSD
U00020408 DiskSpace:512

Set all values of the active property to true. This is done by selecting the JSONPaths of all active properties and then using JSON_SET_VALUE to modify that paths to true.

:SET &JSON_DOC# = '[{"host":"192.168.0.10","active":false},{"host":"192.168.0.20","active":false},{"host":"192.168.0.29","active": true}]'
:SET &HND# = JSON_TO_PROCESS(&JSON_DOC#, '$..active', "PATH" )
:PROCESS &HND#
: SET &STATUS_PATH# = GET_PROCESS_LINE(&HND#)
: SET &RET# = JSON_SET_VALUE(&JSON_DOC#, &STATUS_PATH#, 'true')
:ENDPROCESS
:PRINT &JSON_DOC#

Activation report result:

U00020408 [{"host":"192.168.0.10","active":"true"},{"host":"192.168.0.20","active":"true"},{"host":"192.168.0.29","active":"true"}]

See also: