How can I map jsonb value into a new array in PostgreSQL?

ismatim

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]
a_horse_with_no_name

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I get a map value inside a array from Firestore?

In postgresql, how can I return a boolean value instead of string on a jsonb key?

How can you map an array to a new array based on the key-value pairs in an object?

How to sum a value in a JSONB array in Postgresql?

Can PostgreSQL JOIN on jsonb array objects?

How can I conditionally add elements to a jsonb array?

How to update PostgreSQL array of jsonb

How can I add condition based on select value (JSONB) on PostgreSQL 12?

How can I update all values in an array dynamically in jsonb column?

postgresql: How to update JSONB to add new key in nested array

how insert a new value in a jsonb postgresql array of integer

How can I insert a golang map into PostgreSQL

How can I query a jsonb array to find if it contains a value from a query list?

How can I access a key value in jsonb postgresql?

With Postgres, how can I use UPDATE to insert a new jsonb value if NULL, or amend the json if a value already exists?

How can I push a new value to a sub array in php?

How can I aggregate the results of a jsonb_path_query in postgresql to an array?

How can I pass a new single value map as an argument?

How can I use part of an array value as key for a new array?

Value Query from PostgreSQL jsonb array

Can I sum an array of jsonb in Postgresql with dynamic keys in a select statement?

How can I create an array with key value and set new state?

How can I create new array of similar key value pair?

How to map an array of objects in a a new array with new key value

PostgreSQL find by value in array in jsonb data

How can I calc sum for jsonb in PostgreSQL?

How can I search a row using a value which is in a jsonB column?

How to add new key-value pair in each object of JSONB Array- PostgreSQL

How to map arraytype to Jsonb[] in Postgresql?