How do I get useful data out of a SPARQL endpoint using javascript only?

asked 14 Jan '11, 15:26

rgrp's gravatar image

rgrp ♦♦
501122027
accept rate: 14%


If your javascript is in a webpage hosted on a different domain, you have the usual cross-site request restriction.

So first, how to get the response from the endpoint to your page:

  1. check if the endpoint is CORS-enabled - if so, you can use AJAX
  2. check if the endpoint supports JSONP - is so you can use JSONP (not all endpoints support JSON)
  3. if neither, you can use a proxy webservice like http://morph.talis.com which can take the URI of a SPARQL query and serve JSONP.

If the answer is 3:

  1. Write/generate your SPARQL query and assign it to a variable in your javascript: var query = '{your query here}';.
  2. create a variable for the URI of your query: var queryUri = endpointUri+'?query='+encodeURIComponent(query);
  3. create the URI of the request to the Morph service: var jsonpUri = 'http://morph.talis.com/?data-uri[]='+encodeURIComponent(queryUri)+'&output=jsonp&callback=processSparqlResults';
  4. create a script tag: var newScript = document.createElement('script');
  5. give it a src attribute: newScript.src=jsonpUri;
  6. append the script tag to the document body: document.body.appendChild(newScript);
  7. Make sure you have defined the callback function: function processSparqlResults(data){ console.log(data); }
  8. If you did a SELECT or ASK query, the JSON format used will be http://www.w3.org/TR/rdf-sparql-json-res/ ; if DESCRIBE or CONSTRUCT, it will be http://n2.talis.com/wiki/RDF_JSON_Specification
link
This answer is marked "community wiki".

answered 14 Jan '11, 16:33

keithalexander's gravatar image

keithalexander
31237
accept rate: 0%

edited 14 Jan '11, 22:43

Good point Keith -- fortunately bibliographica is CORS enabled. The other alternative is for the endpoint to support jsonp (though I'm not sure whether this is a standard for how endpoints support sparql).

(14 Jan '11, 19:03) rgrp ♦♦

Here is a nice tutorial from DailyJS.com: A Simple Linked Data and JavaScript Tutorial

link

answered 20 Jan '11, 10:46

stefanw's gravatar image

stefanw ♦
251126
accept rate: 50%

Here's an example piece of javascript (+ html) that talks to the Bibliographica SPARQL endpoint:

http://bibliographica.org/demos/sparql.html

This demo uses the 'figtrees' sparql js library: http://thefigtrees.net/lee/sw/sparql.js

link

answered 14 Jan '11, 15:38

rgrp's gravatar image

rgrp ♦♦
501122027
accept rate: 14%

edited 14 Jan '11, 19:07

I think some SPARQL endpoints can be configured to return JSON, which can be pulled into a web page without cross-domain security restrictions getting in the way.

One appraoch I have used it to run a SPARQL query using YQL and then grab the JSON response. For example, try a query of the form: select * from sparql.search where query=”YOUR_SPARQL_QUERY” and service=”SPARQL_ENDPOINT_URL” in the Yahoo Developer console [http://developer.yahoo.com/yql/console/] (See http://blog.ouseful.info/2010/11/02/accessing-linked-data-in-scraperwiki/ for a couple of examples.)

Another tool I've used is SPARQLproxy [http://data-gov.tw.rpi.edu/ws/sparqlproxy.php] which lets you enter a query and endpoint, and get the result in a variety of formats, inclusing three different JSON formats (SPARQL JSON, MIT Simile/Exhibit JSON and Google Viz API JSON).

link

answered 14 Jan '11, 18:34

psychemedia's gravatar image

psychemedia ♦♦
1.1k323961
accept rate: 11%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×10
×3

Asked: 14 Jan '11, 15:26

Seen: 1,599 times

Last updated: 12 Jul, 08:08

powered by OSQA