Skip to main content

Overview

The ParseJSONResultsPlugin 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.
Example:
Source: ~/workspace/source/src/plugin/parse-json-results/parse-json-results-plugin.ts:11

Usage Example

With SQLite JSON Functions

Per-Query Usage

Methods

transformQuery

No-op implementation. This plugin doesn’t modify queries, only results. Returns: The original, unmodified query node Source: ~/workspace/source/src/plugin/parse-json-results/parse-json-results-plugin.ts:79

transformResult

Recursively parses all JSON strings in the result rows into JavaScript objects and arrays. Parameters:
  • args.queryId - Unique identifier for the query
  • args.result - The query result containing rows to parse
Returns: The result with all JSON strings parsed into objects/arrays Source: ~/workspace/source/src/plugin/parse-json-results/parse-json-results-plugin.ts:83

How It Works

The plugin recursively processes all values in the result rows:
  1. String Detection: Checks if a value is a string starting with [ or {
  2. JSON Parsing: Attempts to parse the string as JSON
  3. Recursive Processing: Recursively processes nested objects and arrays
  4. Type Preservation: Non-JSON values are left unchanged

Parsing Logic

Use Cases

SQLite with JSON Functions

SQLite’s json_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

See Also