Skip to content

pavel-xa/jsonb-extend

 
 

Repository files navigation

JSONB_EXTEND

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
    If a is jsonb object then b should 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

  1. Clone source:
git clone https://github.com/koctep/jsonb-extend.git
cd jsonb-extend
  1. Build and install:
make USE_PGXS=1
make USE_PGXS=1 install
  1. Enable jsonb_extend extension for your database:
CREATE EXTENSION jsonb_extend;

Building a debian package (example for Ubuntu Linux 14.04)

Instead of step 2 do:

  1. Add official PostgreSQL repository to your system if necessary.

  2. Install required dependencies for build:

    sudo apt-get install git debhelper postgresql-server-dev-9.4 devscripts
  3. Build a package:

    debuild -i -us -uc -b
  4. 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

About

PostgreSQL jsonb_extend function

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 92.9%
  • Makefile 3.9%
  • PLpgSQL 3.2%