CREATE EXTENSION jsonbx; select jsonb_pretty('{"a": "test", "b": [1, 2, 3], "c": "test3", "d":{"dd": "test4", "dd2":{"ddd": "test5"}}}'::jsonb); jsonb_pretty ---------------------------- { + "a": "test", + "b": + [ + 1, + 2, + 3 + ], + "c": "test3", + "d": + { + "dd": "test4", + "dd2": + { + "ddd": "test5"+ } + } + } (1 row) select jsonb_concat('{"d": "test", "a": [1, 2]}'::jsonb, '{"g": "test2", "c": {"c1":1, "c2":2}}'::jsonb); jsonb_concat ------------------------------------------------------------------- {"a": [1, 2], "c": {"c1": 1, "c2": 2}, "d": "test", "g": "test2"} (1 row) select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"cq":"l", "b":"g", "fg":false}'; ?column? --------------------------------------------- {"b": "g", "aa": 1, "cq": "l", "fg": false} (1 row) select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"aq":"l"}'; ?column? --------------------------------------- {"b": 2, "aa": 1, "aq": "l", "cq": 3} (1 row) select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"aa":"l"}'; ?column? ------------------------------ {"b": 2, "aa": "l", "cq": 3} (1 row) select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{}'; ?column? ---------------------------- {"b": 2, "aa": 1, "cq": 3} (1 row) select '["a", "b"]'::jsonb || '["c"]'; ?column? ----------------- ["a", "b", "c"] (1 row) select '["a", "b"]'::jsonb || '["c", "d"]'; ?column? ---------------------- ["a", "b", "c", "d"] (1 row) select '["c"]' || '["a", "b"]'::jsonb; ?column? ----------------- ["c", "a", "b"] (1 row) select '["a", "b"]'::jsonb || '"c"'; ?column? ----------------- ["a", "b", "c"] (1 row) select '"c"' || '["a", "b"]'::jsonb; ?column? ----------------- ["c", "a", "b"] (1 row) select '"a"'::jsonb || '{"a":1}'; ERROR: invalid concatnation of jsonb objects select '{"a":1}' || '"a"'::jsonb; ERROR: invalid concatnation of jsonb objects select '["a", "b"]'::jsonb || '{"c":1}'; ?column? ---------------------- ["a", "b", {"c": 1}] (1 row) select '{"c": 1}'::jsonb || '["a", "b"]'; ?column? ---------------------- [{"c": 1}, "a", "b"] (1 row) select '{}'::jsonb || '{"cq":"l", "b":"g", "fg":false}'; ?column? ------------------------------------ {"b": "g", "cq": "l", "fg": false} (1 row) select pg_column_size('{}'::jsonb || '{}'::jsonb) = pg_column_size('{}'::jsonb); ?column? ---------- t (1 row) select pg_column_size('{"aa":1}'::jsonb || '{"b":2}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb); ?column? ---------- t (1 row) select pg_column_size('{"aa":1, "b":2}'::jsonb || '{}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb); ?column? ---------- t (1 row) select pg_column_size('{}'::jsonb || '{"aa":1, "b":2}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb); ?column? ---------- t (1 row) select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'a'); jsonb_delete ------------------ {"b": 2, "c": 3} (1 row) select jsonb_delete('{"a":1}'::jsonb, 'a'); jsonb_delete -------------- {} (1 row) select jsonb_delete('"a"'::jsonb, 'a'); ERROR: cannot delete from scalar select jsonb_delete('{"a":null , "b":2, "c":3}'::jsonb, 'a'); jsonb_delete ------------------ {"b": 2, "c": 3} (1 row) select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'b'); jsonb_delete ------------------ {"a": 1, "c": 3} (1 row) select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'c'); jsonb_delete ------------------ {"a": 1, "b": 2} (1 row) select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'd'); jsonb_delete -------------------------- {"a": 1, "b": 2, "c": 3} (1 row) select '{"a":1}'::jsonb - 'a'::text; ?column? ---------- {} (1 row) select '"a"'::jsonb - 'a'::text; ERROR: cannot delete from scalar select '{"a":1 , "b":2, "c":3}'::jsonb - 'a'::text; ?column? ------------------ {"b": 2, "c": 3} (1 row) select '{"a":null , "b":2, "c":3}'::jsonb - 'a'::text; ?column? ------------------ {"b": 2, "c": 3} (1 row) select '{"a":1 , "b":2, "c":3}'::jsonb - 'b'::text; ?column? ------------------ {"a": 1, "c": 3} (1 row) select '{"a":1 , "b":2, "c":3}'::jsonb - 'c'::text; ?column? ------------------ {"a": 1, "b": 2} (1 row) select '{"a":1 , "b":2, "c":3}'::jsonb - 'd'::text; ?column? -------------------------- {"a": 1, "b": 2, "c": 3} (1 row) select pg_column_size('{"a":1 , "b":2, "c":3}'::jsonb - 'b'::text) = pg_column_size('{"a":1, "b":2}'::jsonb); ?column? ---------- t (1 row) select '["a","b","c"]'::jsonb - 3; ?column? ----------------- ["a", "b", "c"] (1 row) select '["a","b","c"]'::jsonb - 2; ?column? ------------ ["a", "b"] (1 row) select '["a","b","c"]'::jsonb - 1; ?column? ------------ ["a", "c"] (1 row) select '["a","b","c"]'::jsonb - 0; ?column? ------------ ["b", "c"] (1 row) select '["a","b","c"]'::jsonb - -1; ?column? ------------ ["a", "b"] (1 row) select '["a","b","c"]'::jsonb - -2; ?column? ------------ ["a", "c"] (1 row) select '["a","b","c"]'::jsonb - -3; ?column? ------------ ["b", "c"] (1 row) select '["a","b","c"]'::jsonb - -4; ?column? ----------------- ["a", "b", "c"] (1 row) select '{"a":1, "b":2, "c":3}'::jsonb - 3; ?column? -------------------------- {"a": 1, "b": 2, "c": 3} (1 row) select '{"a":1, "b":2, "c":3}'::jsonb - 2; ?column? ------------------ {"a": 1, "b": 2} (1 row) select '{"a":1, "b":2, "c":3}'::jsonb - 1; ?column? ------------------ {"a": 1, "c": 3} (1 row) select '{"a":1, "b":2, "c":3}'::jsonb - 0; ?column? ------------------ {"b": 2, "c": 3} (1 row) select '{"a":1, "b":2, "c":3}'::jsonb - -1; ?column? ------------------ {"a": 1, "b": 2} (1 row) select '{"a":1, "b":2, "c":3}'::jsonb - -2; ?column? ------------------ {"a": 1, "c": 3} (1 row) select '{"a":1, "b":2, "c":3}'::jsonb - -3; ?column? ------------------ {"b": 2, "c": 3} (1 row) select '{"a":1, "b":2, "c":3}'::jsonb - -4; ?column? -------------------------- {"a": 1, "b": 2, "c": 3} (1 row) select jsonb_delete('{"a":1, "b":2, "c":3}'::jsonb, '{d, e}'::text[]); jsonb_delete -------------------------- {"a": 1, "b": 2, "c": 3} (1 row) select jsonb_delete('{"a":1, "b":2, "c":3}'::jsonb, '{b}'::text[]); jsonb_delete ------------------ {"a": 1, "c": 3} (1 row) select jsonb_delete('{"a":{"c":1, "d": 2}, "b":3}'::jsonb, '{a, c}'::text[]); jsonb_delete ------------------------- {"a": {"d": 2}, "b": 3} (1 row) select jsonb_delete('{"a":{"c":1, "d":{"f": 3, "g": 4}}, "b":5}'::jsonb, '{a, d, g}'::text[]); jsonb_delete ---------------------------------------- {"a": {"c": 1, "d": {"f": 3}}, "b": 5} (1 row) select jsonb_delete('{"a":1, "b":2, "c":3}'::jsonb, '{}'::text[]); jsonb_delete -------------------------- {"a": 1, "b": 2, "c": 3} (1 row) select '{"a":1, "b":2, "c":3}'::jsonb - '{d, e}'::text[]; ?column? -------------------------- {"a": 1, "b": 2, "c": 3} (1 row) select '{"a":1, "b":2, "c":3}'::jsonb - '{b}'::text[]; ?column? ------------------ {"a": 1, "c": 3} (1 row) select '{"a":{"c":1, "d":2}, "b":3}'::jsonb - '{a, c}'::text[]; ?column? ------------------------- {"a": {"d": 2}, "b": 3} (1 row) select '{"a":{"c":1, "d":{"f": 3, "g": 4}}, "b":5}'::jsonb - '{a, d, g}'::text[]; ?column? ---------------------------------------- {"a": {"c": 1, "d": {"f": 3}}, "b": 5} (1 row) select '{"a":1, "b":2, "c":3}'::jsonb - '{}'::text[]; ?column? -------------------------- {"a": 1, "b": 2, "c": 3} (1 row) select pg_column_size('{"a":1, "b":2, "c":3}'::jsonb - '{a}'::text[]) = pg_column_size('{"b":2, "c":3}'::jsonb); ?column? ---------- t (1 row) select pg_column_size('{"a":1, "b":2, "c":3}'::jsonb - '{}'::text[]) = pg_column_size('{"a":1, "b":2, "c":3}'::jsonb); ?column? ---------- t (1 row) select jsonb_set('"a"'::jsonb, '{a}', '[1,2,3]'); ERROR: cannot set path in scalar select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '[1,2,3]'); jsonb_set -------------------------------------------------------------------------- {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": [1, 2, 3]} (1 row) select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '[1,2,3]'); jsonb_set ----------------------------------------------------------------------------- {"a": 1, "b": [1, [1, 2, 3]], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null} (1 row) select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,1,0}', '[1,2,3]'); jsonb_set ----------------------------------------------------------------------------- {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [[1, 2, 3], 3]}, "n": null} (1 row) select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '[1,2,3]'); jsonb_set --------------------------------------------------------------------- {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null} (1 row) select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '{"1": 2}'); jsonb_set ------------------------------------------------------------------------- {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": {"1": 2}} (1 row) select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '{"1": 2}'); jsonb_set ---------------------------------------------------------------------------- {"a": 1, "b": [1, {"1": 2}], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null} (1 row) select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,1,0}', '{"1": 2}'); jsonb_set ---------------------------------------------------------------------------- {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [{"1": 2}, 3]}, "n": null} (1 row) select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '{"1": 2}'); jsonb_set --------------------------------------------------------------------- {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null} (1 row) select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '"test"'); jsonb_set -------------------------------------------------------------------------- {"a": 1, "b": [1, "test"], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null} (1 row) select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '{"f": "test"}'); jsonb_set --------------------------------------------------------------------------------- {"a": 1, "b": [1, {"f": "test"}], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null} (1 row) -- empty structure and error conditions for delete and replace select '"a"'::jsonb - 'a'; -- error ERROR: cannot delete from scalar select '{}'::jsonb - 'a'; ?column? ---------- {} (1 row) select '[]'::jsonb - 'a'; ?column? ---------- [] (1 row) select '"a"'::jsonb - 1; -- error ERROR: cannot delete from scalar select '{}'::jsonb - 1 ; ?column? ---------- {} (1 row) select '[]'::jsonb - 1; ?column? ---------- [] (1 row) select '"a"'::jsonb - '{a}'::text[]; -- error ERROR: cannot delete path in scalar select '{}'::jsonb - '{a}'::text[]; ?column? ---------- {} (1 row) select '[]'::jsonb - '{a}'::text[]; ?column? ---------- [] (1 row) select jsonb_set('"a"','{a}','"b"'); --error ERROR: cannot set path in scalar select jsonb_set('{}','{a}','"b"', false); jsonb_set ----------- {} (1 row) select jsonb_set('[]','{1}','"b"', false); jsonb_set ----------- [] (1 row) -- jsonb_set adding instead of replacing -- prepend to array select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{b,-33}','{"foo":123}'); jsonb_set ------------------------------------------------------- {"a": 1, "b": [{"foo": 123}, 0, 1, 2], "c": {"d": 4}} (1 row) -- append to array select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{b,33}','{"foo":123}'); jsonb_set ------------------------------------------------------- {"a": 1, "b": [0, 1, 2, {"foo": 123}], "c": {"d": 4}} (1 row) -- check nesting levels addition select jsonb_set('{"a":1,"b":[4,5,[0,1,2],6,7],"c":{"d":4}}','{b,2,33}','{"foo":123}'); jsonb_set --------------------------------------------------------------------- {"a": 1, "b": [4, 5, [0, 1, 2, {"foo": 123}], 6, 7], "c": {"d": 4}} (1 row) -- add new key select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{c,e}','{"foo":123}'); jsonb_set ------------------------------------------------------------ {"a": 1, "b": [0, 1, 2], "c": {"d": 4, "e": {"foo": 123}}} (1 row) -- adding doesn't do anything if elements before last aren't present select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{x,-33}','{"foo":123}'); jsonb_set ----------------------------------------- {"a": 1, "b": [0, 1, 2], "c": {"d": 4}} (1 row) select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{x,y}','{"foo":123}'); jsonb_set ----------------------------------------- {"a": 1, "b": [0, 1, 2], "c": {"d": 4}} (1 row) -- add to empty object select jsonb_set('{}','{x}','{"foo":123}'); jsonb_set --------------------- {"x": {"foo": 123}} (1 row) --add to empty array select jsonb_set('[]','{0}','{"foo":123}'); jsonb_set ---------------- [{"foo": 123}] (1 row) select jsonb_set('[]','{99}','{"foo":123}'); jsonb_set ---------------- [{"foo": 123}] (1 row) select jsonb_set('[]','{-99}','{"foo":123}'); jsonb_set ---------------- [{"foo": 123}] (1 row)