1

I have PostgreSQL 10.2 table "snapshots" with jsonb column named "data"

{
  "entries": [
    {
      "userName": "John",
      "age": "15"
    },
    {
      "userName": "Max",
      "age": "42"
    }]
}

Need a query to select only userNames from entries in the array. I tried this

select data->'entries'->>'userName' from snapshots;

but of course it's not returning values that I need.

1 Answer 1

2

Unnest the json array with the function jsonb_array_elements() used in a lateral join:

select item->>'userName'
from snapshots
cross join jsonb_array_elements(data->'entries') as item
Sign up to request clarification or add additional context in comments.

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.