|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > JSON - Parser > Parser-Operations > !Document Operations > PSJ. - JSON Operations |
SPR Script Language
PSJ.Parse
Parses a JSON string and returns a document handle.
Intention
The PSJ.Parse command in SPR parses a JSON string, specified by $$JST, into a JSON document and returns its handle. Optionally, it stores the handle in a variable specified by $$RTN. This command is essential for converting raw JSON text into a manipulable document within SPR.
For example, parsing "{\"key\":\"value\"}" creates a new JSON document with a handle that can be used for further operations.
Illustration
📝 Parse JSON String: PSJ.Parse|{\"key\":\"value\"} parses the string into a document handle.
📝 With Return Variable: PSJ.Parse|[1,2,3]|$$DCH parses the array string and stores the handle in $$DCH.
Syntax
PSJ.Parse|$$JST[|$$RTN]
Parameter Explanation
P1 - $$JST - (Variable, String)
The JSON string to parse (e.g., $$JST as "{\"key\":\"value\"}"). Required.
If this Parameter is just one character like "-", then the command will instead take the last HTP-Request Result.
This is an important feature because Unicode content is preserved this way.
Sample:
' Automatically use last HTP-Request results.
PSJ.-|$$RTN
P2 - $$RTN - (Variable, Numeric, Optional)
The variable to store the document handle (e.g., $$RTN as "$$DCH"). Returns a positive handle on success, or 0 if parsing fails. If omitted, the handle is returned but not stored.
Examples
'***********************************
' Sample 1: Parse a JSON Object
PSJ.Parse|{\"key\":\"value\"}
' Parses the JSON string into a document handle
MBX.Ready
'***********************************
' Sample 2: Parse and Store Handle
PSJ.Parse|[1,2,3]|$$DCH
' Parses the JSON array and stores the handle in $$DCH
IVV.$$DCH!0
MBX.Document handle: $$DCH
ELS.
PRT.Error parsing JSON
EIF.
'***********************************
Remarks
- Supports parsing objects, arrays, and values as the root element.
- Returns 0 if parsing fails (e.g., invalid JSON); use PSJ.ErrCode or PSJ.ErrMsg for details.
- Handles UTF-8 BOM and removes it automatically.
Limitations
- $$JST must be a valid JSON string; empty strings or unsupported root elements cause failure.
- Fails if the JSON structure exceeds available memory or document limits.
See also: