1

I'am thinking about storing some data in postgres jsonb data type. There would be a structure like

{"name": "Jhon Smith", "emails": ["[email protected]", "[email protected]"],
 "phones": ["123456789", "987654321"]}.

I know, that i can search this structure like

where contact->'emails' @> '"[email protected]"'::jsonb;

But what I need is to search my data with some LIKE operator, so

where contact->'emails' <SOME_JSON_LIKE_OPERATOR> "smith"';

I can't find if psql have something similar, maybe it does not. So, maybe I can convert contact->'emails' field to Text ('["[email protected]", "[email protected]"]') and then use simple LIKE.. How would you have solved this problem?

1 Answer 1

4

You can expand the json array into a recordset of text and search that in whatever manner you like:

where exists (
    select 1 from json_array_elements_text(contact->'emails')
    where 
        value like "%smith%"
    )
Sign up to request clarification or add additional context in comments.

1 Comment

@donkopotamis Awesome, thank you! That's exactly what i was looking for.

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.