I can create JSON objects using jsonb_build_object
the way I want them. E.g.
SELECT jsonb_build_object('id', id) FROM (SELECT generate_series(1,3) id) objects;
results in
jsonb_build_object
------------------
{"id": 1}
{"id": 2}
{"id": 3}
But when I want to add them to an array, they are wrapped in an additional object, using the column name as key:
SELECT jsonb_build_object(
'foo', 'bar',
'collection', jsonb_agg(collection)
)
FROM (
SELECT jsonb_build_object('id', id)
FROM (
SELECT generate_series(1,3) id
) objects
) collection;
results in
{"foo": "bar", "collection": [{"jsonb_build_object": {"id": 1}}, {"jsonb_build_object": {"id": 2}}, {"jsonb_build_object": {"id": 3}}]}
How can I get
{"foo": "bar", "collection": [{"id": 1}, {"id": 2}, {"id": 3}]}
instead?