2
[
  {
    "name" : "A",
    "key" : "KA"
  },
  {
    "name" : "B",
    "key" : "KB"
  }
]

Given a column containing the above data,

I use select jsonb_path_query_array(column, '$.key') to get the output [KA, KB]

However this doesn't work in Postgres-11. Are there any alternatives for the same ?

2
  • Yeah, and that code worked when I was trying it out locally. However when I tried deploying it on my company's server, I found out that function wasn't available, since my company's currently using version 11. Commented Sep 2, 2021 at 17:20
  • Cool. Thanks mate, appreicate the help :) Commented Sep 2, 2021 at 17:27

1 Answer 1

2

Yes. This yields a Postgres array. Use jsonb_agg instead of array_agg if you need a JSON array.

select array_agg(j ->> 'key')
from jsonb_array_elements(column) t(j);

Update

select 
  (select array_agg(j ->> 'key') from jsonb_array_elements(column) t(j))
from the_table;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Should I replace t with the table name.

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.