5

I'm fairly new to PostgreSQL specially to the whole set of functions it has to manage JSONs. I have this json example where I can grab always one element but, I want to map it to a new array:

select '{"a": {"b":{"c": [{"id":"foo"},{"id":"fuu"}]}}}'::json#>'{a,b,c}';

          column
-----------------------------
 [{"id":"foo"},{"id":"fuu"}]
(1 row)

What I really want is:

 [foo, fuu]

1 Answer 1

10

If you are using Postgres 12 or newer you can use a JSON path query:

select jsonb_path_query_array(the_column, '$.a.b.c[*].id')
from the_table

This assumes that the column is a jsonb column (which it should be). If it's not you need to cast it: the_column::jsonb

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.