PSJ. - JSON Operations

<< Click to Display Table of Contents >>

Navigation:  3. Script Language > JSON - Parser >

PSJ. - JSON Operations

Feature Overview: Mastering JSON Data

Command Index


SPR Script Language

 

PSJ. - Your Robust & Powerful JSON Toolkit

Unlock the full potential of JSON manipulation within your SPR Scripts using the comprehensive PSJ command set.

 

Built upon the high-performance JSW (JSON Wide) library, the PSJ commands provide a seamless interface for interacting with JSON data. Featuring robust Unicode support through internal WSTRING handling and leveraging the advanced WsLnTre B-Tree library for lightning-fast object operations, PSJ empowers you to handle any JSON task with confidence and efficiency.

 

Key Capabilities & Features:

 

Effortless Parsing, Loading & Saving:
Easily create robust JSON structures from SPR strings (PSJ.Parse, PSJ.Create*) or load entire files with automatic encoding detection (UTF-8/16LE/16BE) using PSJ.LoadFile. Persist your work reliably with PSJ.SaveFile (outputting UTF-16LE with BOM).

Comprehensive Value Manipulation:
Get and Set any primitive JSON type (String, Number, Boolean, Null) with dedicated commands (PSJ.GetStr, PSJ.SetNum, etc.) or use the versatile PSJ.SetVal. Check for null explicitly with PSJ.GetNull. Insert complex pre-parsed JSON with PSJ.SetRaw.

Flexible Structure Management:
Manipulate arrays with ease using PSJ.Append* commands and PSJ.DelElem. Manage object content by retrieving keys (PSJ.GetKeys) or removing specific key-value pairs (PSJ.DelKey).
Ensure paths exist and contain the correct container type (Array or Object) using PSJ.SetArrByPath and PSJ.SetObjByPath.

Powerful Path Navigation & Querying:
Navigate complex structures effortlessly using Universal Path Syntax (Dot/Bracket notation like data.items[0].name or JSON Pointer like /data/items/0/name). PSJ.GetByPath finds or creates nodes, while WsLnTre integration ensures rapid lookups within objects via PSJ.FindChild. Query node Type, ChildCount, array children/siblings (ChildAt, ChildIDs, NextSib - *Array Only*).

Advanced Document & Subtree Operations:
Perform deep copies of entire documents (PSJ.DupDoc) or specific subtrees (PSJ.DupTree), correctly handling nested structures and Object Tree cloning. Merge documents, arrays, or objects with PSJ.Merge* commands. Replace nodes entirely using PSJ.ReplNode.

Convenient Utilities & Extraction:
Quickly search for specific key/value pairs throughout a document with PSJ.Search. Perform basic validation (PSJ.Validate) or check for and optionally remove orphaned nodes (PSJ.Repair). Utilize a suite of PSJ.Extract* commands ("One-Shots") to parse a JSON string and retrieve specific data in a single command, ideal for handling API responses. Manage clipboard integration with PSJ.SetClipOutput / PSJ.GetClipOutput.

Robust String & Unicode Handling:
The library correctly handles JSON string escaping and unescaping, including complex Unicode sequences like surrogate pairs, ensuring accurate representation and manipulation of international text and emojis. Input/Output strings are seamlessly handled between SPR's UTF-8 and the library's internal UTF-16.

 

Getting Started

Begin by parsing your JSON data using PSJ.Parse or loading it from a file with PSJ.LoadFile. Then, explore the command chapters linked below to find the specific operations you need.

 

Explore the Commands:

 

Initialization, State & Error Handling

Parsing, Loading & File I/O

Document & Node Lifecycle

Path Navigation & Querying

Value Get/Set Operations

Array Operations

Object Operations

Merge Operations

Serialization

Utility & One-Shot Commands

Understanding JSON Paths

JSON Error Codes

 

Important Notes

 

- Remember to free document handles using PSJ.Free or PSJ.FreeAll when they are no longer needed.

- Input/Output strings in SPR are generally handled as UTF-8, with automatic conversion to/from the library's internal WSTRING (UTF-16LE) format.

- Check return codes and use PSJ.ErrCode / PSJ.ErrMsg to verify operation success.