Skip to content
Advertisement

jsonb LIKE query on nested objects in an array

My JSON data looks like this:

given a text “foo” I want to return all the tuples that have this substring. But I cannot figure out how to write the query for the same.

I followed this related answer but cannot figure out how to do LIKE.
This is what I have working right now:

Instead of passing the whole text of cafe I want to pass ca and get the results that match that text.

Advertisement

Answer

Your solution can be simplified some more:

Or simpler, yet, with jsonb_array_elements() since you don’t actually need the row type (foo) at all in this example:

db<>fiddle here

But that’s not what you asked exactly:

I want to return all the tuples that have this substring.

You are returning all JSON array elements (0-n per base table row), where one particular key ('{payload,details,*,name}') matches (case-sensitively).

And your original question had a nested JSON array on top of this. You removed the outer array for this solution – I did the same.

Depending on your actual requirements the new text search capability of Postgres 10 might be useful.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement