Provides functions for PostgreSQL 9.4 to merge two or more jsonb
values into one value.
Provided functions:
-
jsonb_extend(a jsonb, b jsonb)
a
- jsonb object/array
b
- jsonb object/array/value
Ifa
is jsonb object thenb
should be jsonb object to. -
jsonb_deep_extend(a jsonb, b jsonb)
a
- jsonb object
b
- jsonb object
select jsonb_extend('[1]'::jsonb, '[2]'::jsonb);
jsonb_extend
--------------
[1, 2]
select jsonb_extend('[1]'::jsonb, '2'::jsonb);
jsonb_extend
--------------
[1, 2]
select jsonb_extend('[1]'::jsonb, '{"a": 2}'::jsonb);
jsonb_extend
---------------
[1, {"a": 2}]
SELECT jsonb_extend('{"a": 5, "b": 6}'::jsonb, '{"b": 7, "c": 9}'::jsonb) AS new_jsonb;
new_jsonb
--------------------------
{"a": 5, "b": 7, "c": 9}
SELECT jsonb_extend('{"a": {"b": 6}}'::jsonb, '{"a": {"c": 7}}'::jsonb) AS new_jsonb;
new_jsonb
---------------------------------
{"a": {"c": 7}}
SELECT jsonb_deep_extend(false, '{"a": {"b": 6}}'::jsonb, '{"a": {"c": 7}}'::jsonb) AS new_jsonb;
new_jsonb
-------------------------
{"a": {"b": 6, "c": 7}}
- Clone source:
git clone https://github.com/koctep/jsonb-extend.git
cd jsonb-extend
- Build and install:
make USE_PGXS=1
make USE_PGXS=1 install
- Enable
jsonb_extend
extension for your database:
CREATE EXTENSION jsonb_extend;
Instead of step 2
do:
-
Add official PostgreSQL repository to your system if necessary.
-
Install required dependencies for build:
sudo apt-get install git debhelper postgresql-server-dev-9.4 devscripts
-
Build a package:
debuild -i -us -uc -b
-
Generated .deb will be placed in parent directory. You may install it with command like:
dpkg -i ../postgresql-9.4-jsonb-extend_1.0-1_amd64.deb