Skip to content

json_query

Generate PostgreSQL WHERE clause predicates from JSONB filter objects.

pg_json_query · Generate PostgreSQL WHERE clause predicates from JSONB filter objects.

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
100610json_querypg_json_query0.9FUNCPostgreSQLSQL

PostgreSQL · 10, 11, 12, 13, 14, 15, 16, 17, 18

Relationships
Requiresplpgsql

Usage

Sources:

json_query 0.9 is a pure-SQL query-building extension that turns validated JSONB filter objects into predicates over a registered table or composite row type. It is useful when a SQL function needs an application-supplied set of optional filters without maintaining many nearly identical function variants.

Register a row type and filter it

CREATE EXTENSION json_query;

CREATE TABLE customer (id integer, status text);
SELECT jq_register_row_type('customer');

SELECT *
FROM customer AS c
WHERE jq_filter(c, '{"status": "active", "id__ge": 100}'::jsonb);

jq_filter(anyelement,jsonb) accepts Django-style keys such as id__ge; jq_filter_raw(anyelement,jsonb) accepts an explicit array of field, operator, and value objects. Operators include equality and ordering, membership, pattern matching, and JSONB containment or existence tests. Nested JSONB fields can use -> and ->> path syntax.

Registration lifecycle

jq_register_row_type(text) generates type-specific helper functions. Run jq_unregister_row_type(text) and register again after changing the table definition. Custom column types may also require jq_register_column_type(text). The extension validates field names and operator names, but applications should still authorize which filters callers may use and cap query cost. The README’s published minimum is PostgreSQL 9.4; the reviewed source has no current release compatibility matrix.

Table reader