JSONB_EXTEND
May 20, 2015 ยท View on GitHub
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
Ifais jsonb object thenbshould be jsonb object to. -
jsonb_deep_extend(a jsonb, b jsonb)
a- jsonb object
b- jsonb object
Usage examples
Concat arrays:
select jsonb_extend('[1]'::jsonb, '[2]'::jsonb);
jsonb_extend
--------------
[1, 2]
Append a value to array:
select jsonb_extend('[1]'::jsonb, '2'::jsonb);
jsonb_extend
--------------
[1, 2]
select jsonb_extend('[1]'::jsonb, '{"a": 2}'::jsonb);
jsonb_extend
---------------
[1, {"a": 2}]
Merging two objects
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}}
Installation
- 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_extendextension for your database:
CREATE EXTENSION jsonb_extend;
Building a debian package (example for Ubuntu Linux 14.04)
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