I'm having this jsonb
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 about ERROR: column "bar" does not exist
I'm using Postgres 9.5