Creating JSON using Groovy

I wrote previously about parsing JSON in Groovy. In this article I show a simple example of creating JSON using Groovy. I had to learn about creating JSON using Groovy as I would be using JSON as the response format in the REST API which might be developed sometime in future.

also read:

Creating JSON using Groovy

Groovy 1.8 introduced json package for parsing and building JSON data. JsonSlurper is used for parsing JSON and JsonBuilder is used for creating JSON in Groovy.

Lets see a very simple example of creating a JSON using named arguments:

 
def jsonBuilder = new groovy.json.JsonBuilder()
jsonBuilder.book(
    isbn: '0321774094',
    title: 'Scala for the Impatient',
    author: 'Cay S. Horstmann',
    publisher: 'Addison-Wesley Professional',
)
println("Using just named arguments")
println(jsonBuilder.toPrettyString())

The output:

Using just named arguments
{
  "book": {
    "isbn": "0321774094",
    "title": "Scala for the Impatient",
    "author": "Cay S. Horstmann",
    "publisher": "Addison-Wesley Professional"
  }
}

Creating JSON using an instance of some class
For this lets define a class which would be a container for our data:

 
class MyBook{
    def isbn
    def title
    def author
    def publisher
}

and the code which would create JSON from an instance of MyBook is

 
def myBook = new MyBook(isbn: '0321774094',
                        title: 'Scala for the Impatient',
                        author: 'Cay S. Horstmann',
                        publisher: 'Addison-Wesley Professional')
jsonBuilder(book: myBook)
println("Using an object")
println(jsonBuilder.toPrettyString())

The output:

Using an object
{
    "book": {
        "title": "Scala for the Impatient",
        "publisher": "Addison-Wesley Professional",
        "isbn": "0321774094",
        "author": "Cay S. Horstmann"
    }
}

Creating JSON using a list of instances

 
def myBook = new MyBook(isbn: '0321774094',
  title: 'Scala for the Impatient',
  author: 'Cay S. Horstmann',
  publisher: 'Addison-Wesley Professional')

def myBook2 = new MyBook(isbn: '0976694085',
  title: 'Pragmatic Ajax: A Web 2.0 Primer',
  author: 'Justin Gehtland, Ben Galbraith, Dion Almaer',
  publisher: 'Pragmatic Bookshelf')

def myBook3 = new MyBook(isbn: '1934356050',
  title: 'Pragmatic Thinking and Learning: Refactor Your Wetware',
  author: 'Andy Hunt',
  publisher: 'Pragmatic Bookshelf')

def myBookList = [myBook,myBook2,myBook3]

jsonBuilder(books: myBookList)
println("Using list of objects")
println(jsonBuilder.toPrettyString())

The output for this would be:

Using list of objects
{
  "books": [
    {
      "title": "Scala for the Impatient",
      "publisher": "Addison-Wesley Professional",
      "isbn": "0321774094",
      "author": "Cay S. Horstmann"
    },
    {
      "title": "Pragmatic Ajax: A Web 2.0 Primer",
      "publisher": "Pragmatic Bookshelf",
      "isbn": "0976694085",
      "author": "Justin Gehtland, Ben Galbraith, Dion Almaer"
    },
    {
      "title": "Pragmatic Thinking and Learning: Refactor Your Wetware",
      "publisher": "Pragmatic Bookshelf",
      "isbn": "1934356050",
      "author": "Andy Hunt"
    }
  ]
}

For more details visit the API here and a good article here.

Comments

comments

About Mohamed Sanaulla

In his day job he works on developing enterprise applications using ADF. He is also the moderator of JavaRanch forums and an avid blogger.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better