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