|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > JSON - Parser > Parser-Operations > !Document Operations > PSJ. - JSON Operations |
SPR Script Language
PSJ.MergeDocs
Merges two JSON documents, appending the second into the first.
Intention
The PSJ.MergeDocs command in SPR merges two JSON documents, combining the contents of $$DC2 (source) into $$DC1 (destination). Optionally, it stores the operation’s status in a variable specified by $$RTN. This command is useful for consolidating JSON data from multiple sources into a single document.
For example, merging document 200 into document 100 appends the nodes from 200 into 100, with arrays extended and object keys overwritten as needed.
Illustration
📝 Merge Documents: PSJ.MergeDocs|100|200 merges document 200 into document 100.
📝 With Status Check: PSJ.MergeDocs|300|400|$$STA merges document 400 into 300 and stores the status in $$STA.
Syntax
PSJ.MergeDocs|$$DC1|$$DC2[|$$RTN]
Parameter Explanation
P1 - $$DC1 - (Variable, Numeric)
The handle of the destination JSON document (e.g., $$DC1 as "100"). Required.
P2 - $$DC2 - (Variable, Numeric)
The handle of the source JSON document to merge into $$DC1 (e.g., $$DC2 as "200"). Required.
P3 - $$RTN - (Variable, Numeric, Optional)
The variable to store the status code (e.g., $$RTN as "$$STA"). Returns 1 on success, 0 on failure. If omitted, the status is returned but not stored.
Examples
'***********************************
' Sample 1: Merge Two Documents
PSJ.MergeDocs|100|200
' Merges document 200 into document 100
MBX.Ready
'***********************************
' Sample 2: Merge with Status Check
PSJ.MergeDocs|300|400|$$STA
' Merges document 400 into 300 and checks status
IVV.$$STA!1
MBX.Documents merged successfully
ELS.
PRT.Error merging documents
EIF.
'***********************************
Remarks
- For objects, keys from $$DC2 overwrite matching keys in $$DC1.
- For arrays, elements from $$DC2 are appended to $$DC1.
- Returns 1 on success, 0 on failure; use PSJ.ErrCode for error details (e.g., invalid handle, type mismatch).
Limitations
- Both $$DC1 and $$DC2 must be valid document handles.
- Fails if node types are incompatible for merging (e.g., merging an array into an object).
See also: