Lab: Web APIs and JSON-LD: Difference between revisions

From info216
No edit summary
No edit summary
Line 64: Line 64:
Which approach do you find to be easiest?
Which approach do you find to be easiest?


 
==Code to get started==
<syntaxhighlight>
import requests
import json
import pprint
from rdflib import Graph, Namespace, RDFS
g = Graph()
dbp = Namespace("http://example.org/")
ex = Namespace("http://example.org/")
geo = Namespace("http://www.w3.org/2003/01/geo/wgs84_pos#")
g.bind("ex", ex)
g.bind("dbp", ex)
g.bind("geo", ex)
</syntaxhighlight>


===Useful Reading===
===Useful Reading===

Revision as of 10:13, 22 April 2020

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

Topics

Programming regular (non-semantic) as well as semantic Web APIs (RESTful web services) with JSON and JSON-LD.

We will use Web APIs to retrieve regular JSON data, and then append it with a semantic context (@context). Finally we will parse it with RDFlib.

@context: signifies a JSON object that contains the context (or semantic mapping) for the other objects in the same JSON array. (Similar to namespaces)


Imports

  • import requests
  • import json
  • import pprint
  • from rdflib import Graph


Tasks

Regular JSON web APIs

Write a small program that accesses a regular (non-semantic) web API and download the result. The "json" library in python can be used to load a json string as a json object (json.loads(data)). Use the the prettyprint import to print a readable version of the json object.

The GeoNames web API (http://www.geonames.org/export/ws-overview.html) offers many services. 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 might need to register a username instead of using "demo"). You can register here if you want to: https://www.geonames.org/login. You also need to enable the webservice here: https://www.geonames.org/manageaccount.

You do not have to use the GeoNames web API. There are lots and lots of other web APIs out there. But we preferably want something simple that does not require extensive 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 .

While you are testing and debugging things, it is good to make measures so that you do not need to call the GeoNames or other API over and over. A solution can be writing the returned data to a file, or copying it into a variable.

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}]}"

Lifting JSON to JSON-LD

In python we can represent JSON objects as dictionaries ({}) and JSON Arrays as lists ([]).

So far we have only used plain JSON. Now we want to move to JSON-LD, the semantic version of JSON. Make a new JSON object (dictionary/{} in python) that will contain the context key-value pairs (context_data). This data has to eventually be added to out JSON data, with "@context" as the key and context_data as the value.

Put at least one pair of strings into it. For example, if you used the postcode API, the pair "lat" and "http://www.w3.org/2003/01/geo/wgs84_pos#lat". You can also put the pair "lng" and "http://www.w3.org/2003/01/geo/wgs84_pos#long".

Add this pair too to the context object: "postalcodes" and "http://dbpedia.org/ontology/postalCode".

Add more string pairs, using existing or inventing new terms as you go along, to the context object.

We will now make a RDFlib Graph from the JSON-LD object.

First you need to pip install the json-ld portion of rdflib if you have not already:

pip install rdflib-jsonld

Now, create a new Graph. Then convert the JSON-LD object to a string (use json.dumps() and write it to a file). Then parse the file with Rdflib (g.parse()).

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 RDF Graph. Of course, it is easy to convert the RDFlib graph back into JSON-LD using g.serialize("json-ld")

If You have more time

Try to download a new JSON from a different API and lift its data to the rdflib Graph, without making a context. This mean you must iterate/access each data point that you need with the json library. e.g http://api.geonames.org/weatherJSON?formatted=true&north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo&style=full

Which approach do you find to be easiest?

Code to get started

import requests
import json
import pprint
from rdflib import Graph, Namespace, RDFS
g = Graph()
dbp = Namespace("http://example.org/")
ex = Namespace("http://example.org/")
geo = Namespace("http://www.w3.org/2003/01/geo/wgs84_pos#")
g.bind("ex", ex)
g.bind("dbp", ex)
g.bind("geo", ex)

Useful Reading