1

I have the following query:

SELECT * FROM post p
 , jsonb_array_elements(p.meta #> '{tags, tag}') d
WHERE  d->>'tag' LIKE '%mytag%';

Which isn't returning any results, I have an object in the post table that looks like this:

enter image description here

I'm not sure why this is failing, my understanding was jsonb_array_elements flattens out arrays and by selecting {tags, tag} it would get the tag string

1 Answer 1

1

The path to the nested array is meta #> '{tags}', not meta #> '{tags, tag}', so this should work

select *
from 
    post p,
    jsonb_array_elements(p.meta #> '{tags}') d
where d->>'tag' like '%mytag%';

-- or simpler

select *
from 
    post p,
    jsonb_array_elements(p.meta -> 'tags') d
where d->>'tag' like '%mytag%';
Sign up to request clarification or add additional context in comments.

1 Comment

that was it! I and really appreciate the simpler example - much nicer syntax for json selection

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.