|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > JSON - Parser > Parser-Operations > !Object Operations > PSJ. - JSON Operations |
SPR Script Language
PSJ.GetObj
Retrieves the node ID of a JSON object at a specified path.
Intention
The PSJ.GetObj command in SPR retrieves the node ID of a JSON object located at a specified path ($$PATH) within a document identified by its handle ($$DHA). You can optionally store this ID in a variable (e.g., $$RET). This node ID is a numeric identifier that lets you directly reference the object for further operations, like getting its keys or values.
Think of it as asking your robot, “Where’s this JSON box located?”—and it gives you a number like 5, which you can use to access that box later, either placing it on the Top of Stack (TOS) or storing it in a variable.
Illustration
Consider this JSON document:
{"user": {"name": "Alice", "age": 30}}
- 🔍 PSJ.GetObj|$$DHA|user|$$RET stores the node ID (e.g., "5") of the "user" object in $$RET.
- 🔍 PSJ.GetObj|$$DHA|user places the node ID (e.g., "5") on TOS.
Syntax
PSJ.GetObj|$$DHA|$$PATH[|$$RET]
Parameter Explanation
P1 - $$DHA - (Variable, Numeric)
The document handle identifying the JSON data to search (e.g., $$DHA as "123"). Required. Obtained from commands like PSJ.Parse or PSJ.LoadFile.
P2 - $$PATH - (Variable, String)
The path to the JSON object whose node ID you want (e.g., $$PATH as "user"). Required. Use dot notation (e.g., "data.user") or leave blank for the root object.
P3 - $$RET - (Variable, String, Optional)
The variable to store the node ID as a string (e.g., $$RET). If omitted, the result is returned on Top of Stack (TOS).
Examples
'***********************************
' PSJ.GetObj - Sample 1: Get Root Object ID
'***********************************
PSJ.Parse|{"id": 1, "name": "Test"}|$$DHA
PSJ.GetObj|$$DHA||$$RET
MBX.Node ID: $$RET ' Displays the root object’s node ID (e.g., "1")
PSJ.Free|$$DHA
MBX.Ready
'
'***********************************
' PSJ.GetObj - Sample 2: Get Nested Object ID
'***********************************
PSJ.Parse|{"profile": {"age": 25, "city": "London"}}|$$DHA
PSJ.GetObj|$$DHA|profile|$$RET
PRT.Node ID: $$RET ' Prints the "profile" object’s node ID (e.g., "3")
PSJ.Free|$$DHA
MBX.Ready
'
'***********************************
' PSJ.GetObj - Sample 3: Result on TOS
'***********************************
PSJ.Parse|{"settings": {"theme": "dark"}}|$$DHA
PSJ.GetObj|$$DHA|settings
' Places the "settings" object’s node ID (e.g., "2") on Top of Stack (TOS)
PSJ.Free|$$DHA
MBX.Ready
'
Remarks
- Returns a numeric node ID as a string (e.g., "5") if the path points to an object; otherwise, returns "0".
- Only works on object nodes; other types (e.g., strings, numbers) return "0".
- Use PSJ.GetType to verify the node is an object, or PSJ.GetKeys to list its keys once you have the ID.
Limitations
- Requires a valid document handle; invalid handles return "0".
- If the path doesn’t exist or isn’t an object, you get "0", which might be ambiguous (no object vs. error).
See also: