@> and <@ (containment operators)

This page documents the preview version (v2.21). Preview includes features under active development and is for development and testing only. For production, use the stable version (v2024.1). To learn more, see Versioning.

Purpose: the @> operator tests if the left-hand JSON value contains the right-hand JSON value. The <@ operator tests if the right-hand JSON value contains the left-hand JSON value.

Signatures:

input values:       jsonb @> jsonb
return value:       boolean

and:

input values:       jsonb <@ jsonb
return value:       boolean

Notes: Each of these operators requires that the inputs are presented as jsonb values. There are no json overloads.

do $body$
declare
  j_left  constant jsonb := '{"a": 1, "b": 2}';
  j_right constant jsonb := '{"b" :2}';
begin
  assert
    (j_left @> j_right) and
    (j_right <@ j_left),
 'unexpected';
end;
$body$;