mirror of
https://github.com/postgres/postgres.git
synced 2025-07-28 23:42:10 +03:00
Rename jsonb_replace to jsonb_set and allow it to add new values
The function is given a fourth parameter, which defaults to true. When this parameter is true, if the last element of the path is missing in the original json, jsonb_set creates it in the result and assigns it the new value. If it is false then the function does nothing unless all elements of the path are present, including the last. Based on some original code from Dmitry Dolgov, heavily modified by me. Catalog version bumped.
This commit is contained in:
@ -3079,62 +3079,62 @@ select '{"a":1, "b":2, "c":3}'::jsonb - -4;
|
||||
{"a": 1, "b": 2, "c": 3}
|
||||
(1 row)
|
||||
|
||||
select jsonb_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '[1,2,3]');
|
||||
jsonb_replace
|
||||
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_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '[1,2,3]');
|
||||
jsonb_replace
|
||||
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_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,1,0}', '[1,2,3]');
|
||||
jsonb_replace
|
||||
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_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '[1,2,3]');
|
||||
jsonb_replace
|
||||
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_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '{"1": 2}');
|
||||
jsonb_replace
|
||||
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_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '{"1": 2}');
|
||||
jsonb_replace
|
||||
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_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,1,0}', '{"1": 2}');
|
||||
jsonb_replace
|
||||
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_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '{"1": 2}');
|
||||
jsonb_replace
|
||||
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_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '"test"');
|
||||
jsonb_replace
|
||||
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_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '{"f": "test"}');
|
||||
jsonb_replace
|
||||
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)
|
||||
@ -3218,17 +3218,85 @@ select '[]'::jsonb - '{a}'::text[];
|
||||
[]
|
||||
(1 row)
|
||||
|
||||
select jsonb_replace('"a"','{a}','"b"'); --error
|
||||
ERROR: cannot replace path in scalar
|
||||
select jsonb_replace('{}','{a}','"b"');
|
||||
jsonb_replace
|
||||
---------------
|
||||
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_replace('[]','{1}','"b"');
|
||||
jsonb_replace
|
||||
---------------
|
||||
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)
|
||||
|
||||
|
@ -3079,62 +3079,62 @@ select '{"a":1, "b":2, "c":3}'::jsonb - -4;
|
||||
{"a": 1, "b": 2, "c": 3}
|
||||
(1 row)
|
||||
|
||||
select jsonb_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '[1,2,3]');
|
||||
jsonb_replace
|
||||
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_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '[1,2,3]');
|
||||
jsonb_replace
|
||||
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_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,1,0}', '[1,2,3]');
|
||||
jsonb_replace
|
||||
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_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '[1,2,3]');
|
||||
jsonb_replace
|
||||
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_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '{"1": 2}');
|
||||
jsonb_replace
|
||||
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_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '{"1": 2}');
|
||||
jsonb_replace
|
||||
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_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,1,0}', '{"1": 2}');
|
||||
jsonb_replace
|
||||
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_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '{"1": 2}');
|
||||
jsonb_replace
|
||||
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_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '"test"');
|
||||
jsonb_replace
|
||||
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_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '{"f": "test"}');
|
||||
jsonb_replace
|
||||
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)
|
||||
@ -3218,17 +3218,85 @@ select '[]'::jsonb - '{a}'::text[];
|
||||
[]
|
||||
(1 row)
|
||||
|
||||
select jsonb_replace('"a"','{a}','"b"'); --error
|
||||
ERROR: cannot replace path in scalar
|
||||
select jsonb_replace('{}','{a}','"b"');
|
||||
jsonb_replace
|
||||
---------------
|
||||
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_replace('[]','{1}','"b"');
|
||||
jsonb_replace
|
||||
---------------
|
||||
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)
|
||||
|
||||
|
@ -747,18 +747,18 @@ select '{"a":1, "b":2, "c":3}'::jsonb - -2;
|
||||
select '{"a":1, "b":2, "c":3}'::jsonb - -3;
|
||||
select '{"a":1, "b":2, "c":3}'::jsonb - -4;
|
||||
|
||||
select jsonb_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '[1,2,3]');
|
||||
select jsonb_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '[1,2,3]');
|
||||
select jsonb_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,1,0}', '[1,2,3]');
|
||||
select jsonb_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '[1,2,3]');
|
||||
select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '[1,2,3]');
|
||||
select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '[1,2,3]');
|
||||
select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,1,0}', '[1,2,3]');
|
||||
select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '[1,2,3]');
|
||||
|
||||
select jsonb_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '{"1": 2}');
|
||||
select jsonb_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '{"1": 2}');
|
||||
select jsonb_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,1,0}', '{"1": 2}');
|
||||
select jsonb_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '{"1": 2}');
|
||||
select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '{"1": 2}');
|
||||
select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '{"1": 2}');
|
||||
select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,1,0}', '{"1": 2}');
|
||||
select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '{"1": 2}');
|
||||
|
||||
select jsonb_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '"test"');
|
||||
select jsonb_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '{"f": "test"}');
|
||||
select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '"test"');
|
||||
select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '{"f": "test"}');
|
||||
|
||||
select jsonb_delete('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{n}'::text[]);
|
||||
select jsonb_delete('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{b,-1}'::text[]);
|
||||
@ -780,6 +780,26 @@ select '[]'::jsonb - 1;
|
||||
select '"a"'::jsonb - '{a}'::text[]; -- error
|
||||
select '{}'::jsonb - '{a}'::text[];
|
||||
select '[]'::jsonb - '{a}'::text[];
|
||||
select jsonb_replace('"a"','{a}','"b"'); --error
|
||||
select jsonb_replace('{}','{a}','"b"');
|
||||
select jsonb_replace('[]','{1}','"b"');
|
||||
select jsonb_set('"a"','{a}','"b"'); --error
|
||||
select jsonb_set('{}','{a}','"b"', false);
|
||||
select jsonb_set('[]','{1}','"b"', false);
|
||||
|
||||
-- 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}');
|
||||
-- append to array
|
||||
select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{b,33}','{"foo":123}');
|
||||
-- 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}');
|
||||
-- add new key
|
||||
select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{c,e}','{"foo":123}');
|
||||
-- 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}');
|
||||
select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{x,y}','{"foo":123}');
|
||||
-- add to empty object
|
||||
select jsonb_set('{}','{x}','{"foo":123}');
|
||||
--add to empty array
|
||||
select jsonb_set('[]','{0}','{"foo":123}');
|
||||
select jsonb_set('[]','{99}','{"foo":123}');
|
||||
select jsonb_set('[]','{-99}','{"foo":123}');
|
||||
|
Reference in New Issue
Block a user