10.1.8.2 Creating JSON Views 3.1.7

See the light - agile, industrial strength, rapid web application development made easy

(Quick Reference) # 10.1.8.2 Creating JSON Views - Reference Documentation Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari Version: 3.1.7 ## 10.1.8.2 Creating JSON Views JSON views go into the `grails-app/views` directory and end with the `.gson` suffix. They are regular Groovy scripts and can be opened in any Groovy editor. Example JSON view:
    json.person {
        name "bob"
    }
Tip: To open them in the Groovy editor in Intellij double click on the file and when asked which file to associate it with choose "Groovy" The above JSON view produces:
    {"person":{"name":"bob"}}
There is an implicit `json` variable which is an instance of StreamingJsonBuilder. Example usages:
    json(1,2,3) == "[1,2,3]"
    json { name "Bob" } == '{"name":"Bob"}'
    json([1,2,3]) { n it } == '[{"n":1},{"n":2},{"n":3}]'
Refer to the API documentation on StreamingJsonBuilder for more information about what is possible. | Quick Reference (hide)