Skip to content

DuckDB Extension

JSONata

Query and reshape JSON values in DuckDB with JSONata expressions. Select nested fields, filter arrays, and construct a new JSON object.

35,550
extension loads · last 90 days
On this page
Sample data for the examples

Each browser example starts with these sample tables; sample files are shared across examples on this page. Run this setup first when trying the examples in your own DuckDB session.

-- Fixtures for the jsonata extension cookbook / technical-details / functions.json
-- examples. Each table has a JSON `payload` column with realistic shape.
-- Idempotent: uses CREATE OR REPLACE.

CREATE OR REPLACE TABLE contacts AS
SELECT * FROM (VALUES
  (
    1,
    '{
      "FirstName": "Fred",
      "Surname": "Smith",
      "Account": {"Name": "Firefly", "Tier": "gold"},
      "Phone": [
        {"type": "home",   "number": "0203 544 1234"},
        {"type": "mobile", "number": "077 7700 1234"}
      ],
      "Orders": [
        {"Quantity": 3, "UnitPrice": 12.50},
        {"Quantity": 1, "UnitPrice": 40.00}
      ]
    }'::JSON
  ),
  (
    2,
    '{
      "FirstName": "Jane",
      "Surname": "Doe",
      "Account": {"Name": "Acme", "Tier": "silver"},
      "Phone": [
        {"type": "home",   "number": "0207 555 0100"},
        {"type": "mobile", "number": "077 1234 5678"}
      ],
      "Orders": [
        {"Quantity": 2, "UnitPrice": 5.00}
      ]
    }'::JSON
  )
) AS t(contact_id, payload);

CREATE OR REPLACE TABLE orders AS
SELECT * FROM (VALUES
  (
    1001,
    '{
      "Order": {
        "Product": [
          {"Description": {"Colour": "red"},   "Price": 10.00, "Quantity": 2},
          {"Description": {"Colour": "blue"},  "Price":  5.50, "Quantity": 4}
        ]
      }
    }'::JSON
  ),
  (
    1002,
    '{
      "Order": {
        "Product": [
          {"Description": {"Colour": "green"}, "Price": 7.25, "Quantity": 1}
        ]
      }
    }'::JSON
  )
) AS t(order_id, payload);

CREATE OR REPLACE TABLE invoices AS
SELECT * FROM (VALUES
  (
    1,
    '{
      "items": [
        {"id": "a", "status": "open",   "amount": 250, "price": 250},
        {"id": "b", "status": "closed", "amount":  90, "price":  90},
        {"id": "c", "status": "open",   "amount":  50, "price":  50}
      ]
    }'::JSON
  )
) AS t(invoice_id, payload);

CREATE OR REPLACE TABLE line_items AS
SELECT * FROM (VALUES
  (
    1,
    '{
      "items": [
        {"id": "x", "price": 150},
        {"id": "y", "price":  80},
        {"id": "z", "price": 200}
      ]
    }'::JSON
  )
) AS t(line_item_id, payload);

Transform nested JSON with a dedicated expression language

JSONata is a language for selecting, filtering, aggregating, and reshaping JSON. The extension evaluates a JSONata expression against a JSON value in DuckDB and returns the transformed document.

It is useful when you need to traverse nested arrays or build a new object from several parts of a document. DuckDB’s built-in JSON operators already cover simple field extraction; JSONata gives you another way to express more involved transformations or reuse existing JSONata rules.

Example

Select a mobile number from JSON

The expression filters Phone by type and selects the number from the matching item.

Tested with DuckDB 1.5.6. Browser examples use Haybarn-Wasm 2.0.0-rc12.

SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
  WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL jsonata FROM query_farm;
LOAD jsonata FROM query_farm;

SELECT jsonata(
  'Phone[type="mobile"].number',
  '{"Phone":[{"type":"home","number":"555-0100"},{"type":"mobile","number":"555-0199"}]}'
) AS mobile;

Expected result

mobile
555-0199

JSONata has its own expression syntax. Keep SQL quoting separate from the quotes used inside the JSONata expression and JSON document.

Common tasks

Deep Dive

Technical Details

Reference

Extension Contents

Quick reference to all available functions and settings organized by category.

Configuration
jsonata_max_depth Object type: Setting Maximum evaluation depth of a jsonata() expression, which bounds recursion in user-defined functions.
jsonata_timeout_ms Object type: Setting Maximum wall-clock time in milliseconds of a single jsonata() evaluation (one row).
Functions
jsonata() Object type: Scalar function Evaluates a JSONata expression against a JSON document and returns the result as JSON.
jsonata_validate() Object type: Scalar function Checks whether a string is a syntactically valid JSONata expression, without evaluating it.

API Reference

Function Reference

Configuration

Settings

Configure the jsonata extension behavior using these settings.

jsonata_max_depth

Default Value: 1000

jsonata_timeout_ms

Default Value: 10000

Practical Examples

Cookbook

Real-world recipes and patterns for common use cases.

Platform Support

Compatibility

Extension availability may vary by platform and DuckDB version. Check below to ensure this extension supports your environment before installation.

Quick Facts

Release status Stable
Software License MIT
Pricing Free
Written In C++
Source Available Yes
View on GitHub
Usage
35,550
loads · last 90 days

Platforms

DuckDB 2.0.0

  • Linux x86_64aarch64
  • Linux (musl) Not available
  • macOS Apple Silicon
  • Windows x86_64x86_64 (MinGW)
  • WASM Not available
Compiled binary sizes
Platform Architecture Size
Linux x86_64 4.53 MB
Linux aarch64 3.83 MB
macOS Apple Silicon 3.24 MB
Windows x86_64 10.60 MB
Windows x86_64 (MinGW) 18.34 MB

Compressed download sizes for the release shown above.

DuckDB & Haybarn

Release calendar
  • DuckDB v2.0.0 Query.Farm repository Supported
  • Haybarn v1.5.6 Built-in community repository Supported

Browser examples use Haybarn-Wasm 2.0.0-rc12.

Checked .

Learn and explore

Tutorials and articles

Writing about JSONata: how it works, what changed, and why.