• 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)

Using JsonSlurper Groovy API from Java to parse JSON

May 3, 2012 //  by Mohamed Sanaulla//  Leave a Comment

We have seen here on how to parse JSON in Java using Gson and here on how to parse JSON in Groovy. I also touched upon in brief about Groovy here. The beauty of these JVM languages is that one can invoke these APIs from Java, the only requirement is that you need to have the language jar on the classpath. In our example below we use JsonSlurper in Groovy to parse the JSON documents from Java. Sounds cool right! In one of the talks on Functional Programming, Venkat showed us how we could exploit the Software Transactional Memory feature in Clojure to create concurrent applications in Java with less pain.

also read:

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

With so many JVM languages coming up i.e languages which run on JVM, developers are more becoming ployglot i.e use multiple langauges in their code. Going on same lines, in the below example I show you how we could exploit the JSON API in Groovy and use it in our Java program. So all you readers out there keep in mind to add the language specific jar to your classpath. This jar tells the compiler where to find the required API information. For Groovy you can download the required Groovy Development Kit from here. Let GROOVY_HOME be the home directory of your Groovy installation. You would find a jar in the GROOVY_HOME/embeddable folder which would be named like: groovy-all-.jar. This is the jar which you need to add it to the classpath for your Java code to find the right APIs.
If you are not familiar with JSON, do stop by to read about it here, if you are not familiar with Json parsing in Groovy, do read it over here.
The json document which I used is:

[
{
"id":"6253282",
"name":"Twitter API",
"screen_name":"twitterapi",
"url":"http://dev.twitter.com",
"followers_count":1004641,
"friends_count":33,
"favourites_count":24,
"statuses_count":3277
},
{
"id":"15082387",
"name":"Sanaulla",
"screen_name":"sanaulla",
"url":"http://blog.sanaulla.info",
"followers_count":241,
"friends_count":302,
"favourites_count":41,
"statuses_count":1876

}
]

The Java code which does the parsing is:

import groovy.json.JsonSlurper;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;
import java.util.Map;

public class JavaJsonParserDemo {

public static void main(String [] args){

String jsonSrouce = "/home/mohamed/twitterUser.json";
JsonSlurper jsonParser = new JsonSlurper();
try {
List<Map> parsedData = (List<Map>)jsonParser.parse( new FileReader(jsonSrouce));
for ( Map aObject : parsedData){
System.out.println(aObject);
System.out.println(aObject.get("screen_name"));
}
}catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

If you happen to explore the source of JsonSlurper here, you can see that the parse() method invokes either parseObject() or parseArray(). In our sample JSON it would invoke parseArray(), which would return some List. Looking into parseArray() we can find that the List is actually a List of Maps. Keeping this in mind, the declaration used above is List.
Running this program the output would be:

{id=6253282, favourites_count=24, friends_count=33, name=Twitter API,
screen_name=twitterapi, statuses_count=3277, followers_count=1004641,
url=http://dev.twitter.com}
twitterapi
{id=15082387, favourites_count=41, friends_count=302, name=Sanaulla,
screen_name=sanaulla, statuses_count=1876, followers_count=241,
url=http://blog.sanaulla.info}
sanaulla

Category: GroovyTag: Java, 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: « What’s new in Java 7- Features as part of Project Coin
Next Post: Whats new in Java 7 – try-with-resource construct in Project Coin »

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