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');
Was this page helpful?