|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > JSON - Parser > Parser-Operations > !Node Operations > PSJ. - JSON Operations |
SPR Script Language
PSJ.ReplNode
Replaces an existing node at a specified path with a new node.
Intention
The PSJ.ReplNode command in SPR replaces a node at a specified path ($$PTH) within a JSON document ($$DCH) with a new node identified by $$NID. Optionally, it stores the operation status in a variable specified by $$RTN. This command is useful for updating specific parts of a JSON structure.
For example, replacing the node at "root.key" in document 100 with a new node (ID 50) updates the document accordingly.
Illustration
📝 Replace Node: PSJ.ReplNode|100|root.key|50 replaces the node at "root.key" with node 50.
📝 With Status Check: PSJ.ReplNode|200|data.list[0]|75|$$STA replaces the first list item and stores the status in $$STA.
Syntax
PSJ.ReplNode|$$DCH|$$PTH|$$NID[|$$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 node to replace (e.g., $$PTH as "root.key"). Required.
P3 - $$NID - (Variable, Numeric)
The ID of the new node to replace the existing one (e.g., $$NID as "50"). 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: Replace a Node
PSJ.ReplNode|100|root.key|50
' Replaces the node at "root.key" with node 50 in document 100
MBX.Ready
'***********************************
' Sample 2: Replace with Status Check
PSJ.ReplNode|200|data.list[0]|75|$$STA
' Replaces the first list item with node 75 and checks status
IVV.$$STA!0
MBX.Node replaced successfully
ELS.
PRT.Error replacing node: $$STA
EIF.
'***********************************
Remarks
- Returns 0 on success; non-zero values indicate errors (1 = path not found, 2 = parent not found, 3 = invalid $$NID).
- The original node at $$PTH is removed and replaced by $$NID.
- Supports array indices in the path (e.g., "data.list[0]").
Limitations
- $$DCH must be a valid document handle.
- $$PTH must resolve to an existing node; invalid or non-existent paths return an error.
- $$NID must be a valid node ID; invalid IDs result in failure.
See also: