|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > JSON - Parser > Parser-Operations > !Value Operations > PSJ. - JSON Operations |
SPR Script Language
PSJ.SetVal
Sets a value at a specified path with an explicit JSON type.
Intention
The PSJ.SetVal command in SPR sets a value ($$VAL) at a specified path ($$PTH) within a JSON document ($$DCH) with an explicit JSON type ($$TYP). Optionally, it stores the operation status in $$RTN. This command allows precise control over the data type of the value being set.
For example, setting "root.score" to "42" as a number in document 100 ensures the value is stored with the specified type.
Illustration
📝 Set Number: PSJ.SetVal|100|root.score|42|2 sets "score" to 42 as a number (%JSON_TYPE_NUMBER = 2).
📝 Set String with Status: PSJ.SetVal|200|user.name|John|1|$$STA sets "name" to "John" as a string (%JSON_TYPE_STRING = 1) and checks status.
Syntax
PSJ.SetVal|$$DCH|$$PTH|$$VAL|$$TYP[|$$RTN]
Parameter Explanation
P1 - $$DCH - (Variable, Numeric)
The handle of the JSON document (e.g., $$DCH as "100"). Required.
P2 - $$PTH - (Variable, String)
The dot-separated JSON path to the value (e.g., $$PTH as "root.score"). Required.
P3 - $$VAL - (Variable, String)
The string representation of the value to set (e.g., $$VAL as "42" or "true"). Required.
P4 - $$TYP - (Variable, Numeric)
The JSON type of the value (e.g., $$TYP as "1" for %JSON_TYPE_STRING, "2" for %JSON_TYPE_NUMBER). Required.
P5 - $$RTN - (Variable, Numeric, Optional)
The variable to store the status code (e.g., $$RTN as "$$STA"). Returns 0 on success, non-zero on failure (e.g., 1 = invalid handle). If omitted, the status is returned but not stored.
Examples
'***********************************
' Sample 1: Set a Numeric Value
PSJ.SetVal|100|root.score|42|2
' Sets "score" to 42 as a number in document 100
MBX.Ready
'***********************************
' Sample 2: Set String with Status Check
PSJ.SetVal|200|user.name|John|1|$$STA
' Sets "name" to "John" as a string and checks status
IVV.$$STA!0
MBX.Value set successfully
ELS.
PRT.Error setting value: $$STA
EIF.
'***********************************
Remarks
- Creates intermediate objects in $$PTH if they don’t exist.
- Overwrites existing values at the path with the new type and value.
- Returns 0 on success, non-zero on failure (e.g., 2 = invalid type); use PSJ.ErrCode or PSJ.ErrMsg for details.
Limitations
- $$DCH must be a valid document handle.
- $$TYP must be a valid JSON type (e.g., 1 for string, 2 for number); invalid types cause failure.
- $$VAL must match the specified type (e.g., "42" for number, "true" for boolean).
See also: