GrailsLearner
GrailsLearner

Reputation: 525

How to convert sql result array of arrays to array of objects in grails

I have a simple sql query which returns the result as JSON arrays.But i want the output to be array of objects.How to get the expected format.

def data= cust.executeQuery("select custname,dept from customer")
println("data format:"+data);

Output:

Data:[[X,Sales],[Y,Finance]]

Expected format to be:

Data:[
 {Name:X,Dept:Sales},
 {Name:Y,Dept:Finance}
]

Upvotes: 0

Views: 235

Answers (1)

pawels
pawels

Reputation: 1120

I'm not entirely sure, but I think you can achieve your goal with:

data.collect { [Name: it[0], Dept: it[1]] }

I tested it whith [["X", "Sales"], ["Y", "Finance"]].collect { [Name: it[0], Dept: it[1]] } and it does return a list of maps in given format.

Upvotes: 1

Related Questions