1

I have a JSON column called data in a table called , The table looks something like this:

select * from data_table;

 id |           data   
----+------------------------
  1 | ["a","aa","aaa","aaaa"]
  2 | ["b","bb","bbb","bbbb"]
  3 | ["c","cc","ccc","cccc"]

What I would like to do is query the table for all data_table that matches the 'bbb' value in the data column?

I have tried things like this but to no avail:

SELECT * 
FROM data_table 
WHERE data::text[] @> string_to_array('bbb',',') ;
4
  • As in any line that contains bbb regardless of what is around it you want it to return? So in the example above it would return the second line? Commented Dec 11, 2014 at 11:19
  • some versions of postgres seem to have json query built in: stackoverflow.com/questions/10560394/… Commented Dec 11, 2014 at 11:26
  • See this answer which uses OPENJSON, much better than LIKE. Commented Nov 16, 2017 at 15:57
  • See this answer which is much better than using LIKE. Commented Nov 16, 2017 at 15:59

1 Answer 1

3

Use the % wild card and a LIKE statement

SELECT * FROM data_table WHERE data LIKE '%"bbb%"';
Sign up to request clarification or add additional context in comments.

1 Comment

thanks @Matt... but i want exact match, other rows may contain something like 'bbbbb' or 'abbb'.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.