COALESCE to json array not working in sql function

I am using the sql operator in a postgres db and when I try to coalesece to an empty array it doesn't work as expected. It returns null. This is how I am doing it. Is it wrong?
const productItemsImagesSQLAgg = sql<ProductItemImages[]>`
COALESCE(
JSON_AGG(
JSON_BUILD_OBJECT(
'url', ${images.url},
'key', ${images.key}
)
) FILTER (WHERE ${images.id} IS NOT NULL),
'[]'::JSON
)
`.as('images');
const productItemsImagesSQLAgg = sql<ProductItemImages[]>`
COALESCE(
JSON_AGG(
JSON_BUILD_OBJECT(
'url', ${images.url},
'key', ${images.key}
)
) FILTER (WHERE ${images.id} IS NOT NULL),
'[]'::JSON
)
`.as('images');
1 Reply
JuaniS
JuaniSOP4w ago
as

Did you find this page helpful?