How to select specific value in JSON field

Hello, i'am strugling how to get specific value from json field. I want to get the processor value from the selection_group field This is working: const result = await db.run(sql SELECT supplier, brand, selection_group->>'processor', COUNT("id") as productCount FROM products GROUP BY supplier, brand, selection_group->>'processor'); When i use this: .select(sqlsupplier, brand, selection_group->>'processor', COUNT("id") as productCount) .from(products) .groupBy(products.brand, products.supplier) .$dynamic(); i get the error: Maximum call stack size exceeded and when i use .select({ brand: products.brand, supplier: products.supplier, processor: products.selection_group.processor, count: count(products.id), }) .from(products) .groupBy(products.brand, products.supplier) .$dynamic(); i get error: Cannot convert undefined or null to object the last code is my preferred way of doing this code and here i have to add also a group by on the processor field. i also don't know how to do this. Hopefully someone knows how to solve this problem.
2 Replies
Angelelz
Angelelz12mo ago
You're almost there:
.select({
brand: products.brand,
supplier: products.supplier,
processor: sql`${products.selectionGroup}->>'processor'`.as("processor"),
count: count(products.id).as("productCount"),
})
.from(products)
.groupBy(products.brand, products.supplier, sql`processor`)
.select({
brand: products.brand,
supplier: products.supplier,
processor: sql`${products.selectionGroup}->>'processor'`.as("processor"),
count: count(products.id).as("productCount"),
})
.from(products)
.groupBy(products.brand, products.supplier, sql`processor`)
toby
tobyOP12mo ago
Yes, thank you very much!
Want results from more Discord servers?
Add your server