JSON_SET_VALUE

Use the JSON_SET_VALUE script function to set the value of an existing element in a JSON document.

Syntax

JSON_SET_VALUE (JSON,JSONPath,Value[,Cast])

Parameters

&VALUE# &CAST# &JSON_DOC# after ADD_ITEM
'[1,2,3]' AUTO {"val":[1,2,3]}
'[1,2,3]' STRING {"val":"[1,2,3]"}
'1' AUTO {"val":1}
'1' STRING {"val":"1"}
'1' NUMBER {"val":1}
'1' BOOLEAN {"val":true}
'twelve' AUTO {"val":"twelve"}
'twelve' NUMBER

{"val":""} (remark: invalid casting - JSON document is not modified and a casting error is returned)

Return value

None / Error Code in case of error

Runtime errors

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

Limitations

JSON_SET_VALUE does not allow you to add new items to objects or arrays. Use JSON_ADD_ITEM for this purpose.

PATH_TO_ELEMENT can only target one JSON element that should be set. The path must exist in the document.

JSON documents larger than 1MB are not supported.

style="font-weight: bold Important!

Always test JSONPath expressions in an Automation Engine Script before you put them into production. JSONPath expressions developed in other tools may not work as there is no common JSONPath standard.

Examples:

This example uses the script function enrich a JSON object with new values:

:SET &JSON_DOC# = '{"OS": "Linux","disks": null, "isAvailable": false, "inventoryNumber":123}'
:SET &IS_AVAIL# = 1
:SET &RET# = JSON_SET_VALUE(&JSON_DOC#,'$.disks','[{"GB":2000, "type":"HDD"},{"GB":512, "type":"SSD"}]','JSON')
:SET &RET# = JSON_SET_VALUE(&JSON_DOC#,'$.disks',&IS_AVAIL#,'BOOLEAN')
:SET &RET# = JSON_SET_VALUE(&JSON_DOC#,'$.inventoryNumber','null')
:P &JSON_DOC#

Activation report result:

U00020408 {"OS":"Linux","disks":[{"GB":2000,"type":"HDD"},{"GB":512,"type":"SSD"}],"isAvailable":false,"inventoryNumber":null}

Tip:

Sometimes you need to quote (escape) parts of your JSON documents because they are processed by a separate entity. You can achieve escaped parts by using the cast type string for the quoted JSON sub-document:

:SET &DOC# = '{"deploymentName": "hugeServer", "ansibleConf":""}'

:SET &QUOTED_SUBDOC# = '{"awsZone":"srvaws02"}'

:SET &RESULT# = JSON_SET_VALUE(&DOC#, '$.ansibleConf', &QUOTED_SUBDOC#, 'STRING')

:P &DOC#

!outputs {"deploymentName":"hugeServer","ansibleConf":"{\"awsZone\":\"srvaws02\"}"}

See also:

seealso

JSON Processing