PSJ. - JSON Operations

<< Click to Display Table of Contents >>

Navigation:  3. Script Language > JSON - Parser > Text-Operations >

PSJ. - JSON Operations

PSJ.ExtractChoicesContent

PreviousTopNext

 


SPR Script Language

 

PSJ.ExtractChoicesContent

Extracts content from first choice message.

 

Intention

 

The PSJ.ExtractChoicesContent command in SPR takes a JSON string ($$JSON) and extracts the "content" string from the "message" object within the first item of the "choices" array, storing it in an optional variable ($$RET). It’s like asking your robot to open a JSON inbox, find the first message in a list of replies, and read the main text inside it.

 

For example, from {"choices":[{"message":{"content":"Hello"}}]}, it pulls out "Hello" from the first choice’s message.

 

Illustration

📝 Extract: PSJ.ExtractChoicesContent|{"choices":[{"message":{"content":"Hi"}}]}|$$MSG sets $$MSG to "Hi".
📝 Multi: PSJ.ExtractChoicesContent|{"choices":[{"message":{"content":"Yes"}},{"message":{"content":"No"}}]}|$$TXT sets $$TXT to "Yes" (first only).

 

Syntax

 

PSJ.ExtractChoicesContent|$$JSON[|$$RET]

 

Parameter Explanation

 

P1 - $$JSON - (Variable, String)

The JSON string to parse (e.g., $$JSON as "{\"choices\":[{\"message\":{\"content\":\"Hi\"}}]}"). Required.

 

P2 - $$RET - (Variable, String, Optional)

The variable to store the extracted content string (e.g., $$RET as "$$MSG"). If omitted, the string is returned but not stored.

 

Technical Background

 

This command calls W_JSON_ExtractChoicesContent, which parses a JSON string (jsonText) and extracts the "content" string from the path "choices.0.message.content". It returns the string (e.g., "Hi" from {"choices":[{"message":{"content":"Hi"}}]}) or an empty string if an error occurs (e.g., invalid JSON or missing path). Internally, it uses W_JSON_ParseString to create a handle, W_JSON_GetStringByPath to extract the value, and W_JSON_FreeHandle to clean up. No handle is returned, so no freeing is needed by the caller.

 

Examples

 

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

' Sample 1: Extract Simple Content

PSJ.ExtractChoicesContent|{"choices":[{"message":{"content":"Hi"}}]}|$$MSG

' Print the extracted content

PRT.Content: $$MSG

MBX.Ready

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

' Sample 2: First of Multiple Choices

PSJ.ExtractChoicesContent|{"choices":[{"message":{"content":"Yes"}},{"message":{"content":"No"}}]}|$$TXT

' Print the extracted content

PRT.Content: $$TXT

MBX.Ready

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

' Sample 3: No Return Variable

PSJ.ExtractChoicesContent|{"choices":[{"message":{"content":"Bye"}}]}

' Content returned but not stored

MBX.Ready

 

Remarks

 

- Returns the "content" string from the first "choices" message; no handle is created.

- Returns an empty string if the path "choices.0.message.content" is missing or invalid.

- Use PSJ.ErrCode to diagnose errors if the result is empty.

 

Limitations

 

- $$JSON must be valid JSON with a "choices" array.

- Only extracts from the first choice (index 0).

- Requires the exact path "choices.0.message.content".

 

See also:

 

PSJ.ExtractArrStr

PSJ.ErrCode