CREATE EXTENSION jsonb_delete VERSION '1.0'; -- jsonb deletion from an object should match on key/value SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - '{"a": 4, "b": 2}'::jsonb; ?column? ------------------ {"a": 1, "c": 3} (1 row) -- jsonb deletion from an array should only match on element SELECT '["a", "b", "c"]'::jsonb - '{"a": 4, "b": 2}'::jsonb; ?column? ----------------- ["a", "b", "c"] (1 row) -- jsonb deletion from nested objects should not be part matched SELECT '{"a": 4, "b": 2, "c": 3, "d": {"a": 4}}'::jsonb - '{"a": 4, "b": 2}'::jsonb; ?column? ------------------------- {"c": 3, "d": {"a": 4}} (1 row) -- but a match of all nested values should SELECT '{"a": 4, "b": 2, "c": 3, "d": {"a": 4}}'::jsonb - '{"d": {"a": 4}, "b": 2}'::jsonb; ?column? ------------------ {"a": 4, "c": 3} (1 row) SELECT '{"a":{"b":{"c":[1,[2,3,[4]],{"d":1}]}, "c":[1,2,3,4]}, "d":2}'::jsonb - '{"d":2}'::jsonb; ?column? -------------------------------------------------------------------- {"a": {"b": {"c": [1, [2, 3, [4]], {"d": 1}]}, "c": [1, 2, 3, 4]}} (1 row) -- jsonb nulls are equal SELECT '{"a": 1, "b": 2, "c": null}'::jsonb - '{"a": 4, "c": null}'::jsonb; ?column? ------------------ {"a": 1, "b": 2} (1 row) -- others SELECT '{"a": 4, "b": 2, "c": 3, "d": {"a": false}}'::jsonb - '{"d": {"a": false}, "b": 2}'::jsonb; ?column? ------------------ {"a": 4, "c": 3} (1 row) SELECT '{"a": "test", "b": 2.2, "c": {"a": false}}'::jsonb - '{"a": "test2", "c": {"a": false}, "b": 2.2}'::jsonb; ?column? --------------- {"a": "test"} (1 row) SELECT '{"a": "test", "b": 2.2, "c": {"a": false}, "d":true, "e":[1,2,"a"]}'::jsonb - '{"a": "test2", "b": 2.3, "c": {"a": true}, "d":false, "e":[1,2,3]}'::jsonb; ?column? ------------------------------------------------------------------------- {"a": "test", "b": 2.2, "c": {"a": false}, "d": true, "e": [1, 2, "a"]} (1 row) SELECT '{"a": "test", "b": 2.2, "c": {"a": false}, "d":true, "e":[1,2,"a"]}'::jsonb - '{"a": "test", "b": 2.3, "c": {"a": true}, "d":false, "e":[1,2,3]}'::jsonb; ?column? ------------------------------------------------------------ {"b": 2.2, "c": {"a": false}, "d": true, "e": [1, 2, "a"]} (1 row) SELECT '{"a": "test", "b": 2.2, "c": {"a": false}, "d":true, "e":[1,2,"a"]}'::jsonb - '{"a": "test2", "b": 2.2, "c": {"a": true}, "d":false, "e":[1,2,3]}'::jsonb; ?column? --------------------------------------------------------------- {"a": "test", "c": {"a": false}, "d": true, "e": [1, 2, "a"]} (1 row) SELECT '{"a": "test", "b": 2.2, "c": {"a": false}, "d":true, "e":[1,2,"a"]}'::jsonb - '{"a": "test2", "b": 2.3, "c": {"a": false}, "d":false, "e":[1,2,3]}'::jsonb; ?column? ------------------------------------------------------ {"a": "test", "b": 2.2, "d": true, "e": [1, 2, "a"]} (1 row) SELECT '{"a": "test", "b": 2.2, "c": {"a": false}, "d":true, "e":[1,2,"a"]}'::jsonb - '{"a": "test2", "b": 2.3, "c": {"a": true}, "d":true, "e":[1,2,3]}'::jsonb; ?column? -------------------------------------------------------------- {"a": "test", "b": 2.2, "c": {"a": false}, "e": [1, 2, "a"]} (1 row) SELECT '{"a": "test", "b": 2.2, "c": {"a": false}, "d":true, "e":[1,2,"a"]}'::jsonb - '{"a": "test2", "b": 2.3, "c": {"a": true}, "d":false, "e":[1,"a",2]}'::jsonb; ?column? ------------------------------------------------------------------------- {"a": "test", "b": 2.2, "c": {"a": false}, "d": true, "e": [1, 2, "a"]} (1 row) SELECT '{"a": "test", "b": 2.2, "c": {"a": false}, "d":true, "e":[1,2,"a"]}'::jsonb - '{"a": "test2", "b": 2.3, "c": {"a": true}, "d":false, "e":[1,2,"a"]}'::jsonb; ?column? ------------------------------------------------------- {"a": "test", "b": 2.2, "c": {"a": false}, "d": true} (1 row) SELECT '{"a": "test", "b": 2.2, "c": {"a": false}, "d":true, "e":[1,2,"a"]}'::jsonb - '{"a": "test", "b": 2.2, "c": {"a": true}, "d":false, "e":[1,2,3]}'::jsonb; ?column? -------------------------------------------------- {"c": {"a": false}, "d": true, "e": [1, 2, "a"]} (1 row) SELECT '{"a": "test", "b": 2.2, "c": {"a": false}, "d":true, "e":[1,2,"a"]}'::jsonb - '{"a": "test", "b": 2.2, "c": {"a": false}, "d":false, "e":[1,2,3]}'::jsonb; ?column? ------------------------------- {"d": true, "e": [1, 2, "a"]} (1 row) SELECT '{"a": "test", "b": 2.2, "c": {"a": false}, "d":true, "e":[1,2,"a"]}'::jsonb - '{"a": "test", "b": 2.2, "c": {"a": false}, "d":true, "e":[1,2,3]}'::jsonb; ?column? -------------------- {"e": [1, 2, "a"]} (1 row) SELECT '{"a": "test", "b": 2.2, "c": {"a": false}, "d":true, "e":[1,2,"a"]}'::jsonb - '{"a": "test", "b": 2.2, "c": {"a": false}, "d":true, "e":[1,2,"a"]}'::jsonb; ?column? ---------- {} (1 row) SELECT '["a",2,{"a":1, "b":2}]'::jsonb - '[[1]]'::jsonb; ?column? ---------------------------- ["a", 2, {"a": 1, "b": 2}] (1 row) SELECT '["a",2,{"a":1, "b":2}]'::jsonb - '[{"a":1}]'::jsonb; ?column? ---------------------------- ["a", 2, {"a": 1, "b": 2}] (1 row) SELECT '["a",2,{"a":1, "b":2}]'::jsonb - '[{"a":1, "b":2}]'::jsonb; ?column? ---------- ["a", 2] (1 row) SELECT '["a",2,{"a":1, "b":2}]'::jsonb - '["a"]'::jsonb; ?column? ----------------------- [2, {"a": 1, "b": 2}] (1 row) SELECT '["a",2,{"a":1, "b":2}]'::jsonb - '[2]'::jsonb; ?column? ------------------------- ["a", {"a": 1, "b": 2}] (1 row)