PSJ. - JSON Operations

<< Click to Display Table of Contents >>

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

PSJ. - JSON Operations

PSJ.ExtractChoicesAllContent

PreviousTopNext

 


SPR Script Language

 

PSJ.ExtractChoicesAllContent

Extracts all content strings from choices in a JSON response.

 

Intention

 

The PSJ.ExtractChoicesAllContent command in SPR takes a JSON string ($$JSO), extracts all "content" strings from "message" objects within the "choices" array, and stores them in a numbered array (ARR.No). Optionally, it returns the count of extracted strings in $$RET. It’s like asking your robot to sift through a JSON response and collect all the message contents from a list of choices in one go.

 

For example, from {"choices":[{"message":{"content":"Hi"}},{"message":{"content":"Hello"}}]}, it stores "Hi" and "Hello" in the array.

 

Illustration

📝 Extract: PSJ.ExtractChoicesAllContent|{"choices":[{"message":{"content":"Hi"}},{"message":{"content":"Hello"}}]}|1 stores "Hi" at ARR(1,0) and "Hello" at ARR(1,1).
📝 With Count: PSJ.ExtractChoicesAllContent|{"choices":[{"message":{"content":"Yes"}}]}|2|$$CNT stores "Yes" at ARR(2,0) and sets $$CNT to 1.

 

Syntax

 

PSJ.ExtractChoicesAllContent|$$JSON|$$ARR[|$$RET]

 

Parameter Explanation

 

P1 - $$JSON - (Variable, String)

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

 

P2 - $$ARR - (Variable, Numeric)

The array number (e.g., $$ARR as "1") where content strings are stored using ARR command syntax (e.g., ARR(1,0), ARR(1,1)). Required.

 

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

The variable to store the count of extracted strings (e.g., $$RET as "$$CNT"). Returns -1 on error. If omitted, the count is returned but not stored.

 

Technical Background

 

This command parses jsonText and extracts all "content" strings from "message" objects in the "choices" array into a dynamic WSTRING array (contents()). It returns the count of extracted strings or -1 if an error occurs (e.g., invalid JSON, missing "choices" array). The extracted strings are then converted to STRING and stored in the numbered array  that can be accessed using the ARR.(index) command. No document handle is retained, so no explicit cleanup is required.

 

Examples

 

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

' Sample 1: Extract Multiple Contents

PSJ.ExtractChoicesAllContent|{"choices":[{"message":{"content":"Hi"}},{"message":{"content":"Hello"}}]}|1

...

MBX.Ready

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

' Sample 2: Extract with Count

PSJ.ExtractChoicesAllContent|{"choices":[{"message":{"content":"Yes"}}]}|2|$$CNT

...

MBX.Ready

 

Remarks

 

- Stores strings in ARR($$ARR, index) starting at index 0.

- $$RET is the count of extracted strings or -1 on error.

- Use PSJ.ErrCode to diagnose errors if $$RET is -1.

 

Limitations

 

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

- Each "choices" element must contain a "message" object with a "content" field.

 

See also:

 

PSJ.ExtractChoicesContent

PSJ.ErrCode