fn jsonb_delete_path(v: JsonbRef<'_>, path: ListRef<'_>) -> Result<JsonbVal>
Expand description
Deletes the field or array element at the specified path, where path elements can be either field keys or array indexes.
Examples:
# Basic test case
query T
SELECT '["a", {"b":1}]'::jsonb #- '{1,b}';
----
["a", {}]
# Invalid path
query error path element at position 1 is null
SELECT '["a", {"b":1}]'::jsonb #- array[null];
# Removing non-existent key from an object
query T
SELECT '{"a": 1, "b": 2}'::jsonb #- '{c}';
----
{"a": 1, "b": 2}
# Removing an existing key from an object
query T
SELECT '{"a": 1, "b": 2}'::jsonb #- '{a}';
----
{"b": 2}
# Removing an item from an array by positive index
query T
SELECT '["a", "b", "c"]'::jsonb #- '{1}';
----
["a", "c"]
# Removing an item from an array by negative index
query T
SELECT '["a", "b", "c"]'::jsonb #- '{-1}';
----
["a", "b"]
# Removing a non-existent index from an array
query T
SELECT '["a", "b", "c"]'::jsonb #- '{3}';
----
["a", "b", "c"]
# Path element is not an integer for array
query error path element at position 1 is not an integer: "a"
SELECT '["a", "b", "c"]'::jsonb #- '{a}';
# Path to deeply nested value
query T
SELECT '{"a": {"b": {"c": [1, 2, 3]}}}'::jsonb #- '{a,b,c,1}';
----
{"a": {"b": {"c": [1, 3]}}}
# Path terminates early (before reaching the final depth of the JSON)
query T
SELECT '{"a": {"b": {"c": [1, 2, 3]}}}'::jsonb #- '{a}';
----
{}
# Removing non-existent path in nested structure
query T
SELECT '{"a": {"b": {"c": [1, 2, 3]}}}'::jsonb #- '{a,x}';
----
{"a": {"b": {"c": [1, 2, 3]}}}
# Path is longer than the depth of the JSON structure
query T
SELECT '{"a": 1}'::jsonb #- '{a,b}';
----
{"a": 1}
# Edge case: Removing root
query T
SELECT '{"a": 1}'::jsonb #- '{}';
----
{"a": 1}
# Edge case: Empty array
query T
SELECT '[]'::jsonb #- '{a}';
----
[]
# Edge case: Empty object
query T
SELECT '{}'::jsonb #- '{null}';
----
{}
query error cannot delete path in scalar
SELECT '1'::jsonb #- '{}';