Difference between revisions of "Lab: Web APIs, JSON and JSON-LD"

From Info216
Line 15: Line 15:
 
HashMap and List are parts of the basic Java API (JavaDoc here: https://docs.oracle.com/javase/8/docs/api). The other classes are all available through Jena, but Jena does not include JavaDoc for all of them. The file [[:File:jsonld-java-0.7.0-javadoc.zip]] contains JavaDoc for the Java implementation of JSON-LD (unpack it and open the file index.html in a browser).
 
HashMap and List are parts of the basic Java API (JavaDoc here: https://docs.oracle.com/javase/8/docs/api). The other classes are all available through Jena, but Jena does not include JavaDoc for all of them. The file [[:File:jsonld-java-0.7.0-javadoc.zip]] contains JavaDoc for the Java implementation of JSON-LD (unpack it and open the file index.html in a browser).
  
''Because JSON-LD is quite new, there are not yet many good tutorials available. This lab outline is therefore a little more detailed than the others!''
+
''Tip:'' Newer versions of JSON-LD have been released since INFO216 started. apache-jena-3.2.0 uses a newer JSON-LD library (0.9.0) than the previous verion.
 +
Because JSON-LD is so new, this may make a difference, so please consider upgrading!
 +
 
 +
Also, because JSON-LD is quite new, there are not yet many good tutorials available. This lab outline is therefore a little more detailed than the previous ones!
  
 
==Tasks==
 
==Tasks==

Revision as of 15:51, 9 February 2017

Lab 5: Accessing and lifting Web APIs (RESTful web services)

Topics

Programming semantic Web APIs (RESTful web services) with Jena and JSON-LD

Classes/interfaces

  • HashMap (add, get), List
  • Model (read)
  • JsonUtils (toPrettyString)
  • JsonLdOptions (setExpandContext)
  • JsonLdProcessor (compact, expand, flatten, normalize)
  • IOUtils (toInputStream)

HashMap and List are parts of the basic Java API (JavaDoc here: https://docs.oracle.com/javase/8/docs/api). The other classes are all available through Jena, but Jena does not include JavaDoc for all of them. The file File:jsonld-java-0.7.0-javadoc.zip contains JavaDoc for the Java implementation of JSON-LD (unpack it and open the file index.html in a browser).

Tip: Newer versions of JSON-LD have been released since INFO216 started. apache-jena-3.2.0 uses a newer JSON-LD library (0.9.0) than the previous verion. Because JSON-LD is so new, this may make a difference, so please consider upgrading!

Also, because JSON-LD is quite new, there are not yet many good tutorials available. This lab outline is therefore a little more detailed than the previous ones!

Tasks

Write a small program that accesses the GeoNames web API (http://www.geonames.org/export/ws-overview.html) and download the result. For example, you can use this URL to access more information about Ines' neighbourhood in Valencia: http://api.geonames.org/postalCodeLookupJSON?postalcode=46020&country=ES&username=demo . You can use the getJsonBody method (attached to the end of this message) to write this program. (If you call getJsonBody from the static main method in your program, you must define getJsonBody as static too).

The getJsonBody method returns a JSON object, which is either a Java List or a HashMap. Use the toPrettyString method in the JsonUtils class to format and then print your JSON object.

You do not have to use the GeoNames web API. There are lots and lots of other web APIs out there. But we want something simple that does not require registration (HTTPS can also make things more complex when the certificates are outdated). Here are some examples to get you started if you want to try out other APIs: http://opendata.app.uib.no/ , http://data.ssb.no/api , http://ws.audioscrobbler.com/2.0/ , http://www.last.fm/api /intro , http://wiki.musicbrainz.org/Development/JSON_Web_Service .

Be nice! While you are testing things, write a new method getJsonBodyProxy. This method takes a URL parameter just like the original getJsonBody. But it never connects to that URL. Instead, it returns a jsonObject created locally from a results string you have copied into your program. By letting the rest of your program call the new getJsonBodyProxy instead of getJsonBody while you are debugging your code, you do not need to call the GeoNames API over and over.

Here is an example of a results string you can use, if you have trouble connecting to GeoNames (note that you have to escape all the quotation marks inside the Java string): {\"postalcodes\":[{\"adminCode2\":\"V\",\"adminCode1\":\"VC\",\"adminName2\":\"Valencia\",\"lng\":-0.377386808395386,\"countryCode\":\"ES\",\"postalcode\":\"46020\",\"adminName1\":\"Comunidad Valenciana\",\"placeName\":\"Valencia\",\"lat\":39.4697524227712}]}"

So far we have only used plain JSON. Now we want to move to JSON-LD. Make a new HashMap (and therefore also a JSON object) called context. Add at least one pair of strings to it. For example the pair "lat" and "http://www.w3.org/2003/01/geo/wgs84_pos#lat". You can also add the pair "lng" and "http://www.w3.org/2003/01/geo/wgs84_pos#long". Create a JsonLdOptions object and set its expand context to be the context object with the pair of strings in.

Use the JsonLdProcessor to expand your jsonObject and pretty print the result. Has anything happened? Why/why not?!

Add this pair too to the context object: "postalcodes" and "http://dbpedia.org/ontology/postalCode". Rerun. Has anything happened now? Why/why not?!

Add more string pairs, using existing or inventing new terms as you go along, to the context object and rerun expand. The expanded JSON object lifts the data from the web API. It can be used to provide a semantic version of the original web API.

In addition to expand, try the compact and flatten operations on the JSON object. What do they do?

Go back to the RDF/RDFS programs your wrote in labs 2 and 3. Extend the program so that it adds further information about the post codes of every person in your graph.

We will now make a Jena model from the JSON-LD object. To do this, first create a new default Jena model. Then convert the JSON-LD object to a string (use JsonUtils.toPrettyString). Then turn the string into an input stream (use IOUtils.toInputStream, with "UTF-8" as character set). Then read the input stream into your Jena model (use model.read). (There may be other ways to move from JSON object to Jena models, but this is a simple and straightforward way to start.)

Congratulations - you have now gone through the steps of accessing a web API over the net, lifting the results using JSON-LD, manipulating the in JSON-LD and reading them into a Jena RDF model. Of course, it is easy to convert the Jena model back into JSON-LD using model.write(..., "JSON-LD") ...

 /**    Vi brukeraksesserer vev-API-ene på enklest mulig måte, siden det er Jena og JSON-LD
        som er fokus i INFO216, ikke selve vevprogrammeringen.
 */
   static Object getJsonBody(URL serverAddress) {
       Object jsonObject = null;
       HttpURLConnection connection = null;
       try {
           // send GET request
           connection = null;
           connection = (HttpURLConnection)serverAddress.openConnection();
           connection.setRequestMethod("GET");
           connection.setDoOutput(true);
           connection.setReadTimeout(10000);
           connection.connect();
           // parse JSON reponse
           jsonObject = JsonUtils.fromInputStream(connection.getInputStream());
       } catch (MalformedURLException e) {
           e.printStackTrace();
       } catch (ProtocolException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }
       finally
       {
           // close the connection
           connection.disconnect();
           connection = null;
       }
       return jsonObject;
   }