Gregg
Gregg

Reputation: 35864

render builder json not rendering correctly

I have the following:

render(builder: "json") {
   template(message:'hello', dateCreated:'someDate') {
      resources {
          resource(id: "123")
          resource(id: "456")
      }
   }
}

I'm getting the following in Firebug:

{"template":{"message":"hello","dateCreated":"someDate"}}

I can't figure out why I am not getting the resources collection.

Upvotes: 0

Views: 413

Answers (2)

Dónal
Dónal

Reputation: 187539

I suspect resource(id: "123") is being resolved as a call to the g.resource taglib. Can you try the following instead (or rename resource to something else):

render(builder: "json") {
   template(message:'hello', dateCreated:'someDate') {
      resources {
          'resource'(id: "123")
          'resource'(id: "456")
      }
   }
}

Upvotes: 1

Jarred Olson
Jarred Olson

Reputation: 3243

I usually just create a map with the structure that I want and then render it as JSON.

import grails.converters.JSON
def data = [template: [message: 'hello', dateCreated: 'someDate', resources: [[id: "123"], [id:"456"]]]]
render data as JSON

Not sure if that's the exact structure you're looking for but it provides you with

{"template":{"message":"hello","dateCreated":"someDate","resources":[{"id":"123"},{"id":"456"}]}}

You can see an example here:

http://manbuildswebsite.com/2010/01/29/rendering-json-using-grails-part-1-collections-testing-and-the-jsonbuilder/

Upvotes: 1

Related Questions