1

I have this table:

enter image description here

CREATE TABLE lawyer (
  id SERIAL PRIMARY KEY,
  data jsonb
);

INSERT INTO lawyer (data) VALUES
    ('{"a": 1}'),
    ('{"tags":["derecho", "civil", "laboral", "penal"]}'),
    ('{"tags":["derecho", "penal"]}')
;

What I want is a JSONb query in postgres for when I need to find fir example any entry that contains "civil" OR "derecho"

Jeka
  • 1,600
  • 3
  • 22
  • 36
  • Possible duplicate of [Postgres JSONB: query values in JSON array](http://stackoverflow.com/questions/32357971/postgres-jsonb-query-values-in-json-array) – bjmc Jun 09 '16 at 18:29

2 Answers2

2

Finally found a way to do this:

Store json arrays directly as top level:

INSERT INTO lawyer (data) VALUES
    ('["derecho","laboral","penal"]'),
    ('["derecho", "civil", "laboral", "penal"]'),
    ('["derecho", "penal"]')
;

SELECT * 
FROM lawyer 
WHERE data ? 'penal';

Result:

enter image description here

Jeka
  • 1,600
  • 3
  • 22
  • 36
2

For those looking for an answer for the original data structure, here's the SQL:

SELECT * FROM lawyer WHERE lawyer.data->'tags' ? 'penal'
ChrisJ
  • 2,486
  • 21
  • 40