Overview
TheParseJSONResultsPlugin automatically parses JSON strings in query results into JavaScript objects and arrays. This is useful with database dialects that don’t automatically parse JSON but return JSON strings instead.
Installation
Global Installation
Per-Query Installation
Constructor Options
objectStrategy
Type:'in-place' | 'create'Default:
'in-place'
Controls how arrays and objects are handled during parsing:
'in-place'- Arrays’ and objects’ values are parsed in-place. This is the most time and space efficient option, but can result in runtime errors if some objects/arrays are readonly.'create'- New arrays and objects are created to avoid readonly errors.
Usage Example
With SQLite JSON Functions
Per-Query Usage
Methods
transformQuery
transformResult
args.queryId- Unique identifier for the queryargs.result- The query result containing rows to parse
How It Works
The plugin recursively processes all values in the result rows:- String Detection: Checks if a value is a string starting with
[or{ - JSON Parsing: Attempts to parse the string as JSON
- Recursive Processing: Recursively processes nested objects and arrays
- Type Preservation: Non-JSON values are left unchanged
Parsing Logic
Use Cases
SQLite with JSON Functions
SQLite’sjson_array() and json_object() functions return JSON strings:
MySQL JSON Columns
MySQL may return JSON columns as strings in some configurations:Nested JSON Aggregations
Performance Considerations
- In-place Strategy: Fastest and most memory-efficient, suitable for most use cases
- Create Strategy: Slightly slower but safer when dealing with readonly objects or when you need to preserve original data
- Large Results: Parsing large JSON results can be CPU-intensive; consider using the plugin only for queries that actually return JSON