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"

1

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

SELECT * FROM lawyer WHERE lawyer.data->'tags' ? 'penal'

Comments

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.