© 2026 Hedgehog Software, LLC

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

Mysql JSON_ARRAYAGG as a string instead of a json

Hi guys, how are you ?
I'm trying to use a sub query to get and array of objects but in the result i'm not sure how to get the array and dont' get a string
My query is like this
    const data: PaymentResponse[] = await db
      .select({
        id: parent.id,
        date: parent.date,
        status: parent.status,
        method: parent.method,
        totalAmount: parent.totalAmount,
        contacts: sql<SimpleContactResponse[]>`(
            SELECT JSON_ARRAYAGG(JSON_OBJECT(
                'id', c.id,
                'name', c.name,
                'lastName', c.last_name
            ))
            FROM contact c
            INNER JOIN invoice i ON c.id = i.contact_id
            INNER JOIN payment_invoice pi ON i.id = pi.invoice_id
            WHERE pi.payment_id = p.id
            GROUP BY pi.payment_id
        )`,
        settlements: sql<{
          id: number;
          name: string;
        }[]>`(
            SELECT JSON_ARRAYAGG(JSON_OBJECT(
                'id', s.id,
                'name', s.name
            ))
            FROM settlement s
            INNER JOIN invoice i ON s.id = i.settlement_id
            INNER JOIN payment_invoice pi ON i.id = pi.invoice_id
            WHERE pi.payment_id = p.id
            GROUP BY pi.payment_id
        )`
      })
      .from(parent)
      .where(condition)
      .limit(dto.limit)
      .offset(offset)
      .orderBy(desc(parent.date), desc(parent.id));
    const data: PaymentResponse[] = await db
      .select({
        id: parent.id,
        date: parent.date,
        status: parent.status,
        method: parent.method,
        totalAmount: parent.totalAmount,
        contacts: sql<SimpleContactResponse[]>`(
            SELECT JSON_ARRAYAGG(JSON_OBJECT(
                'id', c.id,
                'name', c.name,
                'lastName', c.last_name
            ))
            FROM contact c
            INNER JOIN invoice i ON c.id = i.contact_id
            INNER JOIN payment_invoice pi ON i.id = pi.invoice_id
            WHERE pi.payment_id = p.id
            GROUP BY pi.payment_id
        )`,
        settlements: sql<{
          id: number;
          name: string;
        }[]>`(
            SELECT JSON_ARRAYAGG(JSON_OBJECT(
                'id', s.id,
                'name', s.name
            ))
            FROM settlement s
            INNER JOIN invoice i ON s.id = i.settlement_id
            INNER JOIN payment_invoice pi ON i.id = pi.invoice_id
            WHERE pi.payment_id = p.id
            GROUP BY pi.payment_id
        )`
      })
      .from(parent)
      .where(condition)
      .limit(dto.limit)
      .offset(offset)
      .orderBy(desc(parent.date), desc(parent.id));

and the result that im getting is
        {
            "id": 2901,
            "date": "2023-10-17T07:00:00.000Z",
            "status": "PAID",
            "method": "TRANSFER",
            "totalAmount": "56460.00",
            "contacts": "[{\"id\": 311, \"name\": \"Maximiliano\", \"lastName\": \"Figueroa\"}]",
            "settlements": "[{\"id\": 64, \"name\": \"Octubre 2023\"}]"
        },
        {
            "id": 2901,
            "date": "2023-10-17T07:00:00.000Z",
            "status": "PAID",
            "method": "TRANSFER",
            "totalAmount": "56460.00",
            "contacts": "[{\"id\": 311, \"name\": \"Maximiliano\", \"lastName\": \"Figueroa\"}]",
            "settlements": "[{\"id\": 64, \"name\": \"Octubre 2023\"}]"
        },

one solution is for loop every result and do a JSON.parse but dosen't feel right
I'm not really sure what to do here
thanks for help
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

json_arrayagg(json_array ...
Drizzle TeamDTDrizzle Team / help
2y ago
Json as a string
Drizzle TeamDTDrizzle Team / help
8mo ago
numeric as number instead of a string
Drizzle TeamDTDrizzle Team / help
13mo ago
json column returning as string
Drizzle TeamDTDrizzle Team / help
6mo ago