Parse JSON Request and Build JSON Response in Grails

This is a summary, how to parse JSON type requests and then build a JSON type response in Grails. With JSON type, i mean requests with header of ‘content-type: application/json’

Parse JSON Requests

Please see post ‘Request Automatic Parsing in Grails

Build JSON Response

From String:
Once its done and ready to respond, here is way to build JSON response from string:

  def responseSome = [
                    'status' : "ok"
  render responseSome as JSON

Here is another way to build JSON resonse:

render(contentType: 'application/json'){
           'status' : "ok"

Both of them do the same thing, build JSON response with header ‘content-type: application/json’

From Collection:
To build JSON response from Collection:

if (!{
            render(contentType: "text/json"){
                def errors = array{
                    for (e in review.errors.allErrors){
                        error field: e.field
                        error message: e.defaultMessage

Here all of the errors are collected from Domain class instance validation and new JSON response is built.

For building JSON response from Domain class or other type, please, see post ‘LocalDate as JSON and other types to custom serialize in Grails

LocalDate AS JSON and other types to custom serialize in Grails

This post describes how to create custom serializers to marshall custom types such as LocalDate and Domain class objects into JSON response. The challange is that the default JSON converter provided in Grails is unable to marshall custom types. In practice, it also useful to define how the associations is marshalled into the JSON response. For example, if there is Review associated with RentalUnit, perhaps, you wish only marshall rentalUnit Name instead Id and so on.

Quick and Dirty

One way is to add custom serializer in the controller at the time of rendering the response into JSON response as follows:

def show() {
   if (result){
   //render result as JSON
            render(contentType: "text/json") {
                def reviews = array {
                    for (b in result) {
                        review title: b.title
                        review dateReceived: b.dateReceived.toString("yyyy-MM-dd")

This will create JSON response with list of review titles and dates review received as following:

[{"title":"Great Place to Stay!"},{"dateReceived":"2010-08-30"},
{"title":"Great Apartment- Highly Recommend!"},{"dateReceived":"2010-11-03"} ...]

Important to Note that ‘dateReceived’ is custom type of org.joda.time.LocalDate. With that, you can see how groovy provides such easy way to serialize into string while Grails elegant way to build it into JSON response.

Organized To Maintain

It may be a case, you would like to do JSON marshalling per Class(Type) instead at Controller level. In another words, there is custom marshaller for each different custom type in our case Domain class. Its good for Domain class like User where you don’t want JSON response to include email, age or any other personal information.
Step 1: Create Custom Marshaller
In src/groovy/, perhaps, good place to add your custom marshaller as follows:

package reviews

import grails.converters.JSON

class ReviewMarshaller {

    void register(){
        JSON.registerObjectMarshaller(Review) {Review review ->
            return [
                    title : review?.title,
                    rentalUnitId : review?.rentalUnit?.id,
                    dateReceived : review?.dateReceived.toString("yyyy-MM-dd")


This registers custom marshaller that defines how the JSON response is going to be constructed when ‘as JSON’ called. It also serializes our custom type – LocalDate

NOTE: Make sure to use Safe Navigation operator(?.), otherwise, the marshaller will break

Step 2: Aggregate Marshallers Before Bootstrapping
Next, all of the custom marshallers are kept in one place before bootstrapping each. This makes the Bootstrap.groovy more clean and independent of each custom Marshaller
Perhaps, the src/groovy/util is good locations to add as follows:

package util.marshalling

class CustomObjectMarshallers {

    List marshallers = []

    def register() {
        marshallers.each{ it.register() }

CustomObjectMarshallers has a list of all marshallers with an API to register each

Step 3: Define Spring Bean
In the conf/Spring/resource.groovy, we define the spring bean for CustomObjectmarshaller, so its available at the bootstrapping

import util.marshalling.CustomObjectMarshallers
import reviews.ReviewMarshaller

// Place your Spring DSL code here
beans = {
     customObjectMarshallers( CustomObjectMarshallers ) {
         marshallers = [
                 new ReviewMarshaller()
                 new SomeOtherMarshaller()

Here we inject all of our custom marshallers as we keep adding more

Step 4: Bootstrapping to Register
At last, we register each custom marshaller at the application start up as follows:

class BootStrap {

    def loadDomainDataService
    def grailsApplication

    def init = { servletContext ->

        def applicationContext = grailsApplication.mainContext

        //register Custom marshallers
        applicationContext.getBean( "customObjectMarshallers" ).register()

As you can see, in Bootstrap.groovy we grab the Spring application context and retrieve our Custom Marshall aggregator – customObjectMarshallers that registers each of our custom marshallers

LocalDate Goes Global

We can follow this way and create custom marshaller just for the custom type – org.joda.time.LocalDate, however. Its already done and available as Joda-Time Plugin

Useful Links: