sql - How to query the existence of a key in PostgreSQL 9.5 JSONB column? -


i have postgresql 9.5 database table jsonb column called attrs. want check existence of particular key within attires column. think can use ? operator, don't understand syntax.

select * cereal attrs ? 'lbs'; 

this returns, "the number of minded parameters < number of parameter markers."

select * cereal attrs ? | array['lbs']; 

this returns, "the number of minded parameters < number of parameter markers."

how query existence of key in jsonb column?

you spot on first query.

the ? operator have limitation examines top-level keys of jsonb value. key looking buried somewhere deeper in json document. can use number of jsonb operators or processing functions drill down key supposed located , test: ->, #>, jsonb_each() or jsonb_array_elements(), among others. use of these , run ? operator on result, like:

select * cereal (attrs -> 'weight') ? 'lbs'; 

...for json document like:

{"ingredients": {...}, "weight": {"lbs": 4, "kg": 1.8}, ...} 

without knowledge of structure of json document won't more specific answer.


Comments