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

Notes:

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:

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:

seealso

JSON Processing