Taking a JSON table with some simple data:
CREATE TABLE jsonthings(d JSONB NOT NULL);
INSERT INTO jsonthings(d) VALUES('{"id":1,"email":"Test1@test.com"}');
INSERT INTO jsonthings(d) VALUES('{"id":2,"email":"Test2@test.com"}');
INSERT INTO jsonthings(d) VALUES('{"id":3,"email":"Test3@test.com"}');
I have a requirement to obtain the rows which case-insensitively match a set of email addresses:
SELECT d FROM jsonthings
WHERE (d->>'email')::CITEXT = ANY(ARRAY['TEST1@TEST.COM','test3@test.com']::CITEXT[]);
However, the data I return should contain the email addresses in the case in which they were passed to the query rather than the case in which they are stored within the database. For example, the above query provides the results:
{"id": 1, "email": "Test1@test.com"}
{"id": 3, "email": "Test3@test.com"}
But I need it to return the results:
{"id": 1, "email": "TEST1@TEST.COM"}
{"id": 3, "email": "test3@test.com"}
How can I obtain the matching item in the array and use it in the results?
Best Answer
First of all, you should mention that you are using the additional module
citext
that provides the case insensitive text data typecitext
, which is not strictly needed here. The solution below only requires standard Postgres 9.4.Key elements are
jsonb_each_text()
to unnestjsonb
values into key/value pairs andjson_object_agg()
to re-assemble the JSON value with changed parts:Result:
How?
(0.) Of yourse your table has a primary key you did not mention. Let's call it
id
.Unnest the text array into individual rows and JOIN to the 'email' value of the
jsonb
column usinglower()
on both sides to make it case insensitive.Decompose the
jsonb
column (only identified rows!) into key/value pairs withjsonb_each_text()
.Replace original 'email' values with the search string with a
CASE
statement.Finally aggregate everything back to a json object with
json_object_agg()
and cast tojsonb
.Aside: For a simple structured form like you present in the example, a normalized schema with scalar Postgres data types would be much more efficient: