© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•3y ago•
5 replies
azteca69

How to filter null values from json_arrayagg

Hey sry if this is a stupid question, I am still kinda new to more complex sql queries.
The problem here is that
pictures
pictures
returns an
array
array
of objects that have all properties null. As far as I know this is a correct behaviour for the left join.
I just wanted to know how I could filter the null values in a good way.
 ctx.db
      .select({
        ...getTableColumns(organizers),
        ratings: {
          rating: sql<number>`AVG(value)`,
          reviews: sql<number>`COUNT(value)`,
        },
        pictures: sql<
          (typeof files.$inferSelect)[]
        >`JSON_ARRAYAGG(JSON_OBJECT("id", ${files}.id, "name", ${files}.name, "url", ${files.url}))`.as(
          "pictures",
        ),
      })
      .from(organizers)
      .leftJoin(files, eq(organizers.id, files.entityId))
      .leftJoin(ratings, eq(organizers.id, ratings.organizerId))
      .groupBy(organizers.id);
 ctx.db
      .select({
        ...getTableColumns(organizers),
        ratings: {
          rating: sql<number>`AVG(value)`,
          reviews: sql<number>`COUNT(value)`,
        },
        pictures: sql<
          (typeof files.$inferSelect)[]
        >`JSON_ARRAYAGG(JSON_OBJECT("id", ${files}.id, "name", ${files}.name, "url", ${files.url}))`.as(
          "pictures",
        ),
      })
      .from(organizers)
      .leftJoin(files, eq(organizers.id, files.entityId))
      .leftJoin(ratings, eq(organizers.id, ratings.organizerId))
      .groupBy(organizers.id);
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

json_arrayagg(json_array ...
Drizzle TeamDTDrizzle Team / help
2y ago
How to filter json column
Drizzle TeamDTDrizzle Team / help
2y ago
Mysql JSON_ARRAYAGG as a string instead of a json
Drizzle TeamDTDrizzle Team / help
3y ago
Drizzle-Seed null values
Drizzle TeamDTDrizzle Team / help
12mo ago