Search code examples
postgresqljsonb

postgresql jsonb case insensitive query


I have a table like:

CREATE TABLE cityData
(
  item character varying,
  data jsonb
);

it contains values like

ITEM        DATA
test1       [{"rank":"1", "city":"New York"},{"rank":"3", "city":"Sidney"}] 
test2       [{"rank":"2", "city":"NEW YORK"},{"rank":"4", "city":"New Delhi"}] 

I need to get number of distinct json objects where city is 'New York' I am using the following query

SELECT  * FROM   cityData t
WHERE ( data @> '[{"city":"New York"}]')
and t.item ilike '%test%';

But this query outputs test1 row. I need to make the query case insensitive so that data @> '[{"city":"New York"}]' matches both New York and NEW YORK


Solution

  • where lower(data::text)::jsonb @> lower('[{"city":"New York"}]')::jsonb