|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > JSON - Parser > Parser-Operations > !Node Operations > PSJ. - JSON Operations |
SPR Script Language
PSJ.FindChild
Finds a child node by key name within a parent object.
Intention
The PSJ.FindChild command in SPR searches for a child node with a specific key name ($$KEY) within a parent object identified by $$PARENT) in a JSON document ($$DOC). If $$PARENT is 0, it uses the document’s root node. Optionally, it stores the child node’s ID in $$RET. It’s like asking your robot to find a specific labeled branch in a JSON tree.
For example, searching for "username" in the root object of document 100 returns the ID of the matching child node.
Illustration
📝 Find in Root: PSJ.FindChild|100|0|username finds the "username" child in the root object of document 100.
📝 With Result: PSJ.FindChild|200|300|data|$$ID finds the "data" child under node 300 in document 200 and stores its ID in $$ID.
Syntax
PSJ.FindChild|$$DOC|$$PARENT|$$KEY[|$$RET]
Parameter Explanation
P1 - $$DOC - (Variable, Numeric)
The identifier of the JSON document (e.g., $$DOC as "100"). Must be a valid document. Required.
P2 - $$PARENT - (Variable, Numeric)
The identifier of the parent object (e.g., $$PARENT as "300"). Use 0 to search the root node. Must be a valid object node. Required.
P3 - $$KEY - (Variable, String)
The exact, case-sensitive key name to find (e.g., $$KEY as "username"). Required.
P4 - $$RET - (Variable, Numeric, Optional)
The variable to store the child node’s ID (e.g., $$RET as "$$ID"). Returns the ID if found, or 0 if not found or an error occurs. If omitted, the result is returned but not stored.
Examples
'***********************************
' Sample 1: Find Child in Root
PSJ.FindChild|100|0|username
' Finds "username" in the root object of document 100
MBX.Ready
'***********************************
' Sample 2: Find Child with Result
PSJ.FindChild|200|300|data|$$ID
' Finds "data" under node 300 in document 200, stores ID in $$ID
IF $$ID > 0 THEN PRT.Child ID: $$ID ELSE PRT.Not Found
MBX.Ready
Remarks
- $$RET returns a positive value (node ID) if the key is found, or 0 if the key doesn’t exist, the parent isn’t an object, or an error occurs.
- The search is case-sensitive.
- Use PSJ.Errcode or PSJ.Errmsg for detailed error information if $$RET is 0.
Limitations
- $$DOC must be a valid JSON document identifier.
- $$PARENT must be 0 (root) or a valid object node.
See also: