I have the following code:
def customers = Customer.findAll()
def json = new JsonBuilder()
json {
customers.each { customer ->
id customer.id
name customer.name
address customer.address
}
}
I'm expecting that the result is an json array of customers, but instead it only contains 1 customer. Note the customers list contains 2 elements.
I saw some other post mentioning to use something like:
customers.collect {
Customer c -> [id: c.id, name: c.name, address: c.address]
}
But this style does not really fit nicely in the builder. E.g. I have to use colons :
to assign values.
Is there another approach without creating groovy objects?