postgresqljsonbpostgres-10jsonb-array-elements

Query to search over array elements inside jsonb PSQL


I have a JSON node on which I have to write a PSQL query, My table schema name(String),tagValues(jsonb). Example tagValue data is given below

Name_TagsTable

 uid |       name(String)|          tagValues(jsonb)
-----+-------------------+-----------------------------
   1 |     myName        |    { "tags": ["xyz","pqr","xyp"]}  

I need a query that returns all rows for a search "pq" made on the tagValues of the table

select * from Name_TagsTable where tagValues->tags contains %pq%  

Solution

  • You can use LIKE operator along with casting JSONB value to a string type such as

    SELECT *
      FROM Name_TagsTable
     WHERE (tagValues->'tags')::TEXT LIKE '%pq%'