2

I'm having thisjsonb array:[{"id": 1, "foo": false}, {"id": 1, "foo": true}]

I have been struggling trying to add another field to all the objects inside. This is the result I'm looking for:

[{"id": 1, "foo": false, "bar": true}, {"id": 1, "foo": true, "bar": true}]

I know I need to write a function but I'm kind of new to PostgreSQL so I'm not sure where to start. This is the closest thread I can find:Postgres/JSON - update all array elements but they're to update an existing object by the key.

Any help or point to the direction is much appreciated.

Edit:

I tried to modified to this function

create or replace function add_elements(arr jsonb)returns jsonb language sql as $$    select jsonb_agg(jsonb_build_object("bar", true))    from jsonb_array_elements(arr) e(e)    $$;

but PostgreSQL complained aboutERROR: column "bar" does not exist

I'm using Postgres 9.5

askedNov 1, 2017 at 21:39
Bao Nguyen's user avatar
2
  • What version of postgres are you targeting?CommentedNov 1, 2017 at 21:50
  • @teppic Sorry, forgot to add, I'm using Postgres 9.5CommentedNov 1, 2017 at 21:51

1 Answer1

1

Use the concatenation operator on elements got fromjsonb_array_elements():

with my_table(arr) as (values    ('[{"id": 1, "foo": false}, {"id": 1, "foo": true}]'::jsonb))select jsonb_agg(elem || '{"bar": true}'::jsonb)from my_table,jsonb_array_elements(arr) elem;                                  jsonb_agg                                  ----------------------------------------------------------------------------- [{"id": 1, "bar": true, "foo": false}, {"id": 1, "bar": true, "foo": true}](1 row)

Your function may look like this:

create or replace function add_elements(arr jsonb, val jsonb)returns jsonb language sql as $$    select jsonb_agg(elem || val)    from jsonb_array_elements(arr) elem    $$;with my_table(arr) as (values    ('[{"id": 1, "foo": false}, {"id": 1, "foo": true}]'::jsonb))select add_elements(arr, '{"bar": true}')from my_table;                                add_elements                                 ----------------------------------------------------------------------------- [{"id": 1, "bar": true, "foo": false}, {"id": 1, "bar": true, "foo": true}](1 row)
answeredNov 1, 2017 at 22:20
klin's user avatar
Sign up to request clarification or add additional context in comments.

1 Comment

Nicely done, it works! I've been coming close but never used json_agg before so it was quite confusing. Thank you very much!

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy 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.