© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•3y ago•
7 replies
Alexandru Comanescu

Postgres json_agg

How can I create this equivalent using drizzle?

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 variant
from
  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.id
group by p.id, b.id
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 variant
from
  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.id
group by p.id, b.id


I cannot find any article in the docs.
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Custom postgres JSON_AGG function help
Drizzle TeamDTDrizzle Team / help
15mo ago
limiting json_agg
Drizzle TeamDTDrizzle Team / help
2y ago
Custom SQL function (json_agg & json_build_object)
Drizzle TeamDTDrizzle Team / help
3y ago
Using json_agg does not format correctly
Drizzle TeamDTDrizzle Team / help
3y ago