Search code examples
sqlpostgresqljsonb

OR clause inside JSON query in Postgres


In Postgres JSONB, is it possible to do something like this:

where (
  description ->'Auditor'->'1'->'Internal|External' is not null
)

Instead of this:

where (
  description ->'Auditor'->'1'->'Internal' is not null
  or
  description ->'Auditor'->'1'->'External' is not null
)

Solution

  • You can check whether a jsonb value has any of a set of keys with ?|:

    where description->'Auditor'->'1' ?| array ['Internal','External']
    

    Relevant documentation