select p.id, p.description, p.name, json_build_object( 'id', b.id, 'name', b.name ) as brand, json_agg( json_build_object( 'id', v.id, 'name', v.name ) ) as variantfrom product as p inner join brand as b on b.id = p.brand_id inner join product_variant as v on v.product_id = p.idgroup by p.id, b.id