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")
                        }
                    }
                }
            }
        }
    }
}

1 Answer 1

1

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

templates = array {
  item {
    template = {
      window = {
        // ...
Sign up to request clarification or add additional context in comments.

2 Comments

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
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.