PSJ. - JSON Operations

<< Click to Display Table of Contents >>

Navigation:  3. Script Language > JSON - Parser > Parser-Operations > !File I/O >

PSJ. - JSON Operations

PSJ.LoadFile

PreviousTopNext


SPR Script Language

 

PSJ.LoadFile

Loads a JSON file, returns a new document handle.

 

Intention

 

The PSJ.LoadFile command in SPR loads a JSON file from disk, specified by $$FIL, into a new JSON document handle. Optionally, it stores the resulting document handle in a variable specified by $$RTN. This command is essential for importing JSON data from a file into the SPR environment for further processing.

 

For example, loading "C:\Data\example.json" creates a new document handle, which can then be used to manipulate the JSON data.

 

Illustration

📝 Load a JSON File: PSJ.LoadFile|C:\Data\example.json loads the file into a new document handle.
📝 With Return Variable: PSJ.LoadFile|C:\Data\test.json|$$DCH stores the document handle in $$DCH.

 

Syntax

 

PSJ.LoadFile|$$FIL[|$$RTN]

 

Parameter Explanation

 

P1 - $$FIL - (Variable, String)

The path and filename of the JSON file to load (e.g., $$FIL as "C:\Data\example.json"). Required.

 

P2 - $$RTN - (Variable, Numeric, Optional)

The variable to store the document handle (e.g., $$RTN as "$$DCH"). Returns a positive value (1 to %JSON_Max_Doc) on success, or 0 on failure. If omitted, the handle is returned but not stored.

 

Examples

 

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

' Sample 1: Load a JSON File

PSJ.LoadFile|C:\Data\example.json

' Loads "example.json" into a new document handle

MBX.Ready

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

' Sample 2: Load and Store Document Handle

PSJ.LoadFile|C:\Data\test.json|$$DCH

' Loads "test.json" and stores the handle in $$DCH

IVV.$$DCH!0

 MBX.Document loaded: $$DCH

ELS.

 PRT.Error loading file

EIF.

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

 

Remarks

 

- Returns a document handle (1 to %JSON_Max_Doc) on success, or 0 on failure (e.g., file not found, invalid JSON).

- Use PSJ.ErrCode or PSJ.ErrMsg to diagnose failures if $$RTN is 0.

- The file is read entirely into memory before parsing.

 

Limitations

 

- $$FIL must be a valid, accessible file path with a readable JSON file.

- Fails if the file content is not valid JSON.

 

See also:

 

PSJ.SaveFile

PSJ.ErrCode

PSJ.ErrMsg