PSJ. - JSON Operations

<< Click to Display Table of Contents >>

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

PSJ. - JSON Operations

PSJ.ChildAt

PreviousTopNext


SPR Script Language

 

PSJ.ChildAt

Retrieves the child node identifier at a specified 1-based index under a parent node.

 

Intention

 

The PSJ.ChildAt command in SPR retrieves the identifier of a child node at a specified 1-based index ($$INDEX) under a parent node identified by $$PARENT). Optionally, it stores the result in $$RET. It’s like asking your robot to pick out a specific branch from a tree in your JSON structure.

 

For example, if a parent node with ID "100" has three children, requesting index "2" returns the ID of the second child.

 

Illustration

📝 Get Child ID: PSJ.ChildAt|100|2 returns the ID of the second child of node 100.
📝 With Result: PSJ.ChildAt|200|1|$$CHILD stores the ID of the first child of node 200 in $$CHILD.

 

Syntax

 

PSJ.ChildAt|$$PARENT|$$INDEX[|$$RET]

 

Parameter Explanation

 

P1 - $$PARENT - (Variable, Numeric)

The identifier of the parent node (e.g., $$PARENT as "100"). Must be a valid node with children. Required.

 

P2 - $$INDEX - (Variable, Numeric)

The 1-based index of the child to retrieve (e.g., $$INDEX as "1" for the first child). Required.

 

P3 - $$RET - (Variable, Numeric, Optional)

The variable to store the result (e.g., $$RET as "$$CHILD"). Returns the child node ID or 0 if the index is invalid or the parent has no children. If omitted, the result is returned but not stored.

 

Examples

 

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

' Sample 1: Get Second Child

PSJ.ChildAt|100|2

' Returns the ID of the second child of node 100

MBX.Ready

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

' Sample 2: Store First Child ID

PSJ.ChildAt|200|1|$$CHILD

' Stores the ID of the first child of node 200 in $$CHILD

IF $$CHILD > 0 THEN PRT.Child ID: $$CHILD ELSE PRT.No Child

MBX.Ready

 

Remarks

 

- $$INDEX is 1-based (e.g., 1 is the first child, 2 is the second, etc.).

- $$RET returns the child node ID (greater than 0) or 0 if the index is out of bounds or the parent has no children.

- Use PSJ.ErrCode for additional error information if needed.

 

Limitations

 

- $$PARENT must be a valid node identifier with children.

- $$INDEX must be within the range of the parent’s children (1 to the number of children).

 

See also:

 

PSJ.AddChild

PSJ.ErrCode