Skip to content

pgjq

Use jq in Postgres

pgjq · Use jq in Postgres

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
4150pgjqpgjq0.1.0UTILMITC
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d-rNoYesNoYesyesno
build with jq-devel

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXTPIGSTY0.1.019 18 17 16 15pgjq-
RPMPIGSTY0.1.019 18 17 16 15pgjq_$v-
DEBPIGSTY0.1.019 18 17 16 15postgresql-$v-pgjq-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
el8.aarch64N/APIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
el9.x86_64N/APIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
el9.aarch64N/APIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
el10.x86_64N/APIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
el10.aarch64N/APIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
d12.x86_64N/APIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
d12.aarch64N/APIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
d13.x86_64N/APIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
d13.aarch64N/APIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u22.x86_64N/APIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u22.aarch64N/APIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u24.x86_64N/APIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u24.aarch64N/APIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u26.x86_64N/APIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u26.aarch64N/APIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0

Source

pig build pkg pgjq;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

pig repo add pgsql -u   # add both repo and update cache

Install this extension with pig:

pig install pgjq;		# install by package name

pig install pgjq -v 18;   # install for PG 18
pig install pgjq -v 17;   # install for PG 17
pig install pgjq -v 16;   # install for PG 16
pig install pgjq -v 15;   # install for PG 15

Create this extension with:

CREATE EXTENSION pgjq;

Usage

pgjq: Use jq JSON processing language in PostgreSQL

Provides a jqprog data type for jq programs and a jq() function to execute them on jsonb objects.

Basic Filtering

SELECT jq('[{"bar": "baz", "balance": 7.77}]'::jsonb, '.[0].bar');
-- "baz"

Using the @@ Operator

SELECT '[{"bar": "baz"}]' @@ '.[0].bar'::jqprog;
-- "baz"

Complex Programs

SELECT jq('[true,false,[5,true,[true,[false]],false]]',
          '(..|select(type=="boolean")) |= if . then 1 else 0 end');
-- [1, 0, [5, 1, [1, [0]], 0]]

SELECT jq('[1,5,3,0,7]', '(.[] | select(. >= 2)) |= empty');
-- [1, 0]

Passing Arguments

Pass dynamic arguments as a jsonb object, referenced as $var:

SELECT jq(
    '{"jobs": [{"id": 9, "ok": true}, {"id": 100, "ok": false}]}'::jsonb,
    '.jobs[] | select(.ok == $ok and .id == 100) | .',
    '{"ok": false}'
);

Chaining with jsonpath

SELECT jq('[{"cust":"baz","active":true,"trans":{"balance":100}}]',
          '(.[] | select(.active == true))') - '{trans}' @> '{"cust": "baz"}';
-- t

Working with Files

SELECT jq(pg_read_file('/path/to/data.json'), '.[]');

Table reader