How to add brackets into group_concat using knex

Hey guys I'm using knex with MySQL. I want to add brackets to my field. I'm using group_concat but I get an answer without brackets

knex('cardex').rightJoin('cardex_agelimit' , 'cardex.id' , 'cardex_agelimit.CardexId')
.select( knex.raw('group_concat( DISTINCT (ages.AgeTitle) ) as ages , group_concat( DISTINCT (days.DayName) ) as days '))
.then(data=>{ console.log(data) }).catch(err=>{})

and the result looks like this :

{
"ages" : "Adult,Teenager"
"days" : "Mon,Tue"
}

but I'm looking for a way to show it like:

{
"ages" : ["Adult","Teenager"]
"days" : ["Mon","Tue"]
}

Any idea?


Solution 1:

You could manually add the square brackets around the values:

concat('[', group_concat(distinct ages.AgeTitle), ']')  as ages,
concat('[', group_concat(distinct ages.DayName ), ']')  as days

But bottom line: if you want a json output, you can generate it directly from the database - with the one limitation that json aggregate functions do not support distinct. If that's acceptable for you, then:

json_object(
    'ages', json_arrayagg(ages.AgeTitle),
    'days', json_arrayagg(ages.DayName)
)