• 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
    • Join Us (JBC)
  • 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
    • Join Us (JBC)

Parsing JSON using Groovy

April 30, 2012 //  by Mohamed Sanaulla//  Leave a Comment

In our previous post we saw how we used Gson API to parse the Json using Java. Not spending too much time on the introduction, I would want to straight away dive into the same parsing which can be done using Groovy language. Groovy is a scripting language which runs on the JVM. It can inter-operate with the Java APIs and compiles down into byte code which can then be executed on the JVM.

also read:

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

In this example as well we would make use of the same json document described here.
Groovy support for Json was added in the 1.8 version when a new groovy.json package was added. So you dont have to use an external API for parsing the json documents. JsonSlurper parses the given json document into a data structure of lists and maps. Due to this conversion the name=value pairs in the json document can be accessed by object.name notation where object represents the json object and name represents the name of the attribute.
The model bean in this case is defined as:

class GroovyUser{
def id
def screenName
def name
def url
def followersCount
def friendsCount
def favouritesCount
def statusesCount

GroovyUser(id,
screenName,
name,
url,
followersCount,
friendsCount,
favouritesCount,
statusesCount){
this.id = id
this.screenName = screenName
this.name = name
this.url = url
this.followersCount = followersCount
this.friendsCount = friendsCount
this.favouritesCount = favouritesCount
this.statusesCount = statusesCount

}

def String toString(){
return "Name: "+this.name+"n"+
"ScreenName:"+this.screenName+"n"+
"Followers: "+this.followersCount+"n"+
"Friends: "+this.friendsCount+"n"+
"Favourites: "+this.favouritesCount+"n"+
"Statuses: "+this.statusesCount+"n"
}
}

As Groovy can be used a scripting language, the following code need not be compiled instead one could directly run the code by:

$ groovy filename.groovy

You would have to download and install the required binaries and libraries for running the groovy programs. More can be found at the official site.

import groovy.json.JsonSlurper

def jsonSlurper = new JsonSlurper();

/*
Read the JSON from the file system
*/
def reader = new BufferedReader(
new FileReader("/home/mohamed/twitterUser.json"))
def parsedData = jsonSlurper.parse(reader)
def usersList = new ArrayList<GroovyUser>()
parsedData.each {
aUser ->
def user = new GroovyUser(aUser.id,
aUser.screen_name,
aUser.name,
aUser.url,
aUser.followers_count,
aUser.friends_count,
aUser.favourites_count,
aUser.statuses_count)
usersList.add(user)
}

usersList.each {aUser -> println aUser}

There are few new things to note here:

  • As Groovy supports closures, we are making use of the methods like “each” which is provided by the Groovy API on collections. The each method would for each element in the collection, execute the block of code provided with the each method. Something like-myCollection.each{ aData -> println aData }
  • Semi colons are optional
  • Type details for the variables are optional, so you can see that the variables have been declared by using “def” keyword but no type is declared.
  • Look at the tremendous decrease in the number of lines of code written above. Parsing Json in Groovy is such a pleasure.

On a closing note, the difference in the lines of code in Java and Groovy might tempt few of them to learn the syntax of the language. I am sure learning groovy is pretty easy as it has a short learning curve. Over the last few years there has been a transition to polyglot programming and with the advent of new languages which run on the JVM, they can inter-operate with an existing java application with no additional overhead.
We would over the coming days try to provide a few getting started like tutorials for Groovy and other JVM languages like Scala.

Category: GroovyTag: 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: « Parsing JSON using Java and GSON library
Next Post: Invoking RESTful Web Service using API in java.net and GSON »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

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