• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us

Creating JSON using Groovy

June 22, 2012 //  by Mohamed Sanaulla

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:

  • Groovy Tutorials
  • JSON Tutorials
  • Developing Groovy based web application and deploying to Google App Engine

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.

Category: JavaTag: JSON

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.

Previous Post: « Introduction to Java Agents
Next Post: How to use ByteBuffer in Java? »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Initialize an Array in Java

Introduction to Java Server Faces (JSF)

Introduction to Java 6.0 New Features, Part–1

Java 6.0 Features Part – 2 : Pluggable Annotation Processing API

Introduction to Java Server Faces(JSF) HTML Tags

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact