PSJ. - JSON Operations

<< Click to Display Table of Contents >>

Navigation:  3. Script Language > JSON - Parser > Parser-Operations > !Value Operations >

PSJ. - JSON Operations

PSJ.SetNum

PreviousTopNext


SPR Script Language

 

PSJ.SetNum

Sets or updates a numeric value in a JSON object at a specified path.

 

Intention

 

The PSJ.SetNum command in SPR sets or updates a numeric value ($$VAL) at a specified path ($$PTH) within a JSON document identified by $$DCH. Optionally, it stores the operation status in a variable specified by $$RTN. This command is useful for initializing or modifying numeric properties in a JSON structure.

 

For example, setting "root.object.key" to 42.5 in document 100 creates or updates the numeric value at that path.

 

Illustration

📝 Set Number: PSJ.SetNum|100|root.object.key|42.5 sets "key" to 42.5.
📝 With Status Check: PSJ.SetNum|200|data.value|-10|$$STA sets "value" to -10 and stores the status in $$STA.

 

Syntax

 

PSJ.SetNum|$$DCH|$$PTH|$$VAL[|$$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 path where the numeric value should be set (e.g., $$PTH as "root.object.key"). Required.

 

P3 - $$VAL - (Variable, Numeric)

The numeric value to set (e.g., $$VAL as "42.5"). Can be an integer or decimal. Required.

 

P4 - $$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 = path not found). If omitted, the status is returned but not stored.

 

Examples

 

'***********************************

' Sample 1: Set a Numeric Value

PSJ.SetNum|100|root.object.key|42.5

' Sets "key" to 42.5 in document 100

MBX.Ready

'***********************************

' Sample 2: Set Number with Status Check

PSJ.SetNum|200|data.value|-10|$$STA

' Sets "value" to -10 and checks status

IVV.$$STA!0

 MBX.Number set successfully

ELS.

 PRT.Error setting number: $$STA

EIF.

'***********************************

 

Remarks

 

- Creates intermediate objects in $$PTH if they don’t exist.

- Overwrites existing values at the final key if it’s not an array.

- Returns 0 on success, non-zero on failure (e.g., 1 = path not found); use PSJ.ErrMsg for error details.

 

Limitations

 

- $$DCH must be a valid document handle.

- $$PTH must be a valid object path; array indices (e.g., "list[0]") are not supported.

- Fails if the parent node is not an object or if node creation fails.

 

See also:

 

PSJ.SetBool

PSJ.SetStr

PSJ.ErrMsg