Have you ever wanted to apply a function to a jsonb
object, both dynamically and recursively?
No? Well, here's a Postgres extension that allows you to do just that:
select jsonb_apply(doc jsonb, func text[, variadic "any" args1_n]);
select jsonb_apply('{
"id": 1,
"name": "John",
"messages": [
"hello"
]
}', 'upper');
jsonb_apply
--------------------------------------------------
{"id": 1, "name": "JOHN", "messages": ["HELLO"]}
(1 row)
select jsonb_apply('{
"id": 1,
"name": "John",
"messages": [
"hello"
]
}', 'replace', 'hello', 'bye');
jsonb_apply
------------------------------------------------
{"id": 1, "name": "John", "messages": ["bye"]}
(1 row)
select jsonb_apply('{
"id": 1,
"name": " John ",
"messages": [
" hELLo "
]
}', 'btrim');
jsonb_apply
--------------------------------------------------
{"id": 1, "name": "John", "messages": ["hELLo"]}
(1 row)
select jsonb_apply('"abc~@~def~@~ghi"', 'split_part', '~@~', 2);
jsonb_apply
-------------
"def"
(1 row)
cd /tmp
git clone https://github.com/Florents-Tselai/jsonb_apply.git
cd jsonb_apply
make
make install # may need sudo
create extension jsonb_apply;
select jsonb_apply(doc jsonb, func text[, variadic "any" args1_n]);
Currently only functions with signature like func(text, arg1 "any", ...) → text
are supported.
The first argument arg0
should be text
and is taken as the jsonb
string value (or array element) currently being processed.
The variadic
arguments (if any) will be passed to func
as args1...argsn
.
Bot the function name and the types of variadic
arguments will be used to search for the appropriate function in the catalog.