Using JsonSlurper Groovy API from Java to parse JSON

SHARE & 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:

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

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.

Trackbacks

  1. [...] we make use of the java.net.URL class to obtain the XML Response and then use XmlSlurper (remember JsonSlurper?) to parse the XML to get the required [...]

Speak Your Mind

*

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