1

Say I have a JSONB object like this:

{"First":"Joe", "Last":"Smith", "Age": "29", "cat":"meow"}

I want to be able to find this JSONB object if I search just:

{"First":"Joe", "Age":"29"}

I tried this with a single attribute and it worked:

SELECT * FROM mytable WHERE name @> lower('{"First": "Joe"}')::jsonb

I tried this with two attributes and it did not work:

SELECT * FROM mytable WHERE name @> lower('{"First": "Joe", "Last":"Smith"}')::jsonb

What am I missing? I figured based on the documentation this should work

2
  • What is not working? Did you receive an error? I tried your query in postgres 11 and it seems to work fine. Commented Jun 24, 2019 at 2:43
  • I do not get the row as expected Commented Jun 24, 2019 at 2:57

1 Answer 1

1

Remove the lower(), @> is case-sensitive.

SELECT * FROM mytable WHERE name @> '{"First": "Joe", "Last":"Smith"}'::jsonb

If you want to make it a case-insensitive search, use lower() on the textual value of the JSON and put all attributes to match in lowercase (or may be apply lower() too )

SELECT * FROM mytable WHERE lower(name::text)::jsonb 
        @> '{"first": "joe", "last":"smith"}'::jsonb

DEMO

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.