Rendering JSON in grails Rendering JSON in grails json json

Rendering JSON in grails


The way the JSON object is being built is quite obscure. What I like doing to render JSON responses in grails is creating a map or list in groovy and then use the render method just to convert it to JSON.

Doing the transformation of the rowResult's inside the render method makes it quite confusing, I'd rather go for something like this

def results = db.rows(query).collect { rowResult ->    b(rowResult.name, c, d) }render(contentType: 'text/json') {[    'results': results,    'status': results ? "OK" : "Nothing present"]}

I think it's more readable, and even shorter. This snippet gets you the desired result: no objects inside the results array, just strings.

Note the use of rows, which returns a list of RowResult's, eliminating the need to get it from the ResultSet's. The list is used to collect the transformed value a by calling b on each row's name. Collecting the elements doesn't imply creating a map (like in the { "A":"value1"} JSON you were getting), just the same @will-buck achieved with the << operator on a new, empty list.

All we do with the render method is declaring the text/json content type and passing it a literal map containing the keys results and status, which you want to write to the response. The conditional operator is used to concisely determine the status. It could also be used like this, by means of the JSON converter @will-buck also mentioned:

def responseData = [    'results': results,    'status': results ? "OK" : "Nothing present"]render responseData as JSON


Only off by a little bit :) Just need to change

aMap.put("A", a)

to be a collection or list, rather than a map. so something like

def aList = []aList << a

Will get you what you want!

As a sidenote, there is a JSON converter in grails that will do that string building for you. Look into it here


This should be enough to render a JSON from controller:

render results as grails.converters.JSON