Function jsonb_contains

Source
fn jsonb_contains(left: JsonbRef<'_>, right: JsonbRef<'_>) -> bool
Expand description

Does the first JSON value contain the second?

Examples:

# Simple scalar/primitive values contain only the identical value:
query B
SELECT '"foo"'::jsonb @> '"foo"'::jsonb;
----
t

# The array on the right side is contained within the one on the left:
query B
SELECT '[1, 2, 3]'::jsonb @> '[1, 3]'::jsonb;
----
t

# Order of array elements is not significant, so this is also true:
query B
SELECT '[1, 2, 3]'::jsonb @> '[3, 1]'::jsonb;
----
t

# Duplicate array elements don't matter either:
query B
SELECT '[1, 2, 3]'::jsonb @> '[1, 2, 2]'::jsonb;
----
t

# The object with a single pair on the right side is contained
# within the object on the left side:
query B
SELECT '{"product": "PostgreSQL", "version": 9.4, "jsonb": true}'::jsonb @> '{"version": 9.4}'::jsonb;
----
t

# The array on the right side is not considered contained within the
# array on the left, even though a similar array is nested within it:
query B
SELECT '[1, 2, [1, 3]]'::jsonb @> '[1, 3]'::jsonb;
----
f

# But with a layer of nesting, it is contained:
query B
SELECT '[1, 2, [1, 3]]'::jsonb @> '[[1, 3]]'::jsonb;
----
t

# Similarly, containment is not reported here:
query B
SELECT '{"foo": {"bar": "baz"}}'::jsonb @> '{"bar": "baz"}'::jsonb;
----
f

# A top-level key and an empty object is contained:
query B
SELECT '{"foo": {"bar": "baz"}}'::jsonb @> '{"foo": {}}'::jsonb;
----
t

# This array contains the primitive string value:
query B
SELECT '["foo", "bar"]'::jsonb @> '"bar"'::jsonb;
----
t

# This exception is not reciprocal -- non-containment is reported here:
query B
SELECT '"bar"'::jsonb @> '["bar"]'::jsonb;
----
f

# Object is not primitive:
query B
SELECT '[1, {"a":2}]'::jsonb @> '{"a":2}';
----
f

# Array can be nested:
query B
SELECT '[1, [3, 4]]'::jsonb @> '[[3]]';
----
t

# Recursion shall not include the special rule of array containing primitive:
query B
SELECT '{"a": [3, 4]}'::jsonb @> '{"a": 3}';
----
f