pgjq
Use jq in Postgres
Repository
Florents-Tselai/pgJQ
https://github.com/Florents-Tselai/pgJQ
Source
pgjq-0.1.0.tar.gz
pgjq-0.1.0.tar.gz
Overview
| Package | Version | Category | License | Language |
|---|---|---|---|---|
pgjq | 0.1.0 | UTIL | MIT | C |
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 4150 | pgjq | No | Yes | No | Yes | Yes | Yes | - |
| Related | pgjwt pg_protobuf jsquery sparql gzip bzip zstd http |
|---|
build with jq-devel
Version
| Type | Repo | Version | PG Ver | Package | Deps |
|---|---|---|---|---|---|
| EXT | PIGSTY | 0.1.0 | 1817161514 | pgjq | - |
| RPM | PIGSTY | 0.1.0 | 1817161514 | pgjq_$v | - |
| DEB | PIGSTY | 0.1.0 | 1817161514 | postgresql-$v-pgjq | - |
Build
You can build the RPM / DEB packages for pgjq using pig build:
pig build pkg pgjq # build RPM / DEB packages
Install
You can install pgjq directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:
pig repo add pgsql -u # Add repo and update cache
Install the extension using pig or apt/yum/dnf:
pig install pgjq; # Install for current active PG version
pig ext install -y pgjq -v 18 # PG 18
pig ext install -y pgjq -v 17 # PG 17
pig ext install -y pgjq -v 16 # PG 16
pig ext install -y pgjq -v 15 # PG 15
pig ext install -y pgjq -v 14 # PG 14
dnf install -y pgjq_18 # PG 18
dnf install -y pgjq_17 # PG 17
dnf install -y pgjq_16 # PG 16
dnf install -y pgjq_15 # PG 15
dnf install -y pgjq_14 # PG 14
apt install -y postgresql-18-pgjq # PG 18
apt install -y postgresql-17-pgjq # PG 17
apt install -y postgresql-16-pgjq # PG 16
apt install -y postgresql-15-pgjq # PG 15
apt install -y postgresql-14-pgjq # PG 14
Create Extension:
CREATE EXTENSION pgjq;
Usage
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'), '.[]');
Feedback
Was this page helpful?
Thanks for the feedback! Please let us know how we can improve.
Sorry to hear that. Please let us know how we can improve.