I would like to render a complex type, using the JSON render method in Grails, similar to the JSON output below:
{
"authors": [{
"id": 1,
"name": "Author 1",
"books": [{
"id": 1,
"name": "Book 1"
}, {
"id": 2,
"name": "Book 2"
}]
}, {
"id": 2,
"name": "Author 2",
"books": [{
"id": 1,
"name": "Book 1"
}, {
"id": 2,
"name": "Book 2"
}]
}]
}
And I've tried doing this with the following code, where Author and Book are domain classes containing the properties id and name and where Author hasMany Books (association).
def results = Authors.list()
render(contentType:"text/json") {
authors = array {
for(a in results) {
author id:a.id, name:a.name, books: array = {
def bookresults = Book.findAllByAuthor(a)
for(b in bookresults) {
book id:b.id, name:b.name
}
}
}
}
}
It works fine with authors alone, but not when I try to iterate through each author's books and render those as well, the code fail.
Any ideas?
Updated question with final code
Thanks to Dave's answer I ended up with the following code that works as expected:
def authors = []
for (a in Author.list()) {
def books = []
def author = [id:a.id, name:a.name, books:books]
for (b in Book.findAllByAuthor(a)) {
def book = [id:b.id, name:b.name]
books << book
}
authors << author
}
def items = [authors:[authors]]
render items as JSON