1

Is there a way to nest JSON arrays using the JSONBuilder in Groovy? More explicitly, I have a Grails application that needs to render something like this:

{
    "event": {
        "type": "1.0",
        "templates": [
            {
                "template":{
                    "window": {
                        "type1": "id-1",
                        "type2": "id-2"
                    },
                    "object": {
                        "id-1": {
                            "type": "classA",
                            "others": [
                                {
                                    "var": "thing1",
                                    "mixed": "no"
                                }
                            ]
                        },
                        "id-2": {
                            "type": "classB",
                            "others": [
                                {
                                    "var": "thing1",
                                    "mixed": "yes"
                                }
                            ]
                        }
                    }
                }
            }
        ]
    }
}

I am having some trouble getting my Grails controller to build this using the render function as well as explicitly using a JSONBuilder in a service.

Everything seems to work except that the "template" object inside the "templates" array is not getting rendered. Here is the code that is doing the rendering:

render(contentType: "text/json") {
    event {
        type = "1.0"
        templates = array {
            template = {
                window = {
                    type1 = "id-1"
                    type2 = "id-2"
                }
                object = {
                    "${ 'id-1' }" {
                        type = "classA"
                        others = array {
                            otherArr(var:"thing1", mixed:"yes")
                        }
                    }
                    "${ 'id-2' }" {
                        type = "classB"
                        others = array {
                            otherArr(var:"thing1", mixed:"yes")
                        }
                    }
                }
            }
        }
    }
}
anthonylawson
  • 761
  • 9
  • 24

1 Answers1

1

You're missing a level inside the array closure. Try this:

templates = array {
  item {
    template = {
      window = {
        // ...
Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • That didn't seem to work for me. I get the following error message: `Misplaced key: expected mode of KEY but was OBJECT. Stacktrace follows: Message: Misplaced key: expected mode of KEY but was OBJECT Line | Method ->> 199 | value in grails.converters.JSON` – anthonylawson Jul 05 '12 at 16:05
  • Sorry, my mistake. My original suggestion didn't work. After a bit more experimenting I've come up with something that works and edited my answer accordingly – Ian Roberts Jul 05 '12 at 17:54