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 ♦♦
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 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 = '[]='+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 ; if DESCRIBE or CONSTRUCT, it will be
This answer is marked "community wiki".

answered 14 Jan '11, 16:33

keithalexander's gravatar image

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 A Simple Linked Data and JavaScript Tutorial


answered 20 Jan '11, 10:46

stefanw's gravatar image

stefanw ♦
accept rate: 50%

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

This demo uses the 'figtrees' sparql js library:


answered 14 Jan '11, 15:38

rgrp's gravatar image

rgrp ♦♦
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 where query=”YOUR_SPARQL_QUERY” and service=”SPARQL_ENDPOINT_URL” in the Yahoo Developer console [] (See for a couple of examples.)

Another tool I've used is SPARQLproxy [] 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).


answered 14 Jan '11, 18:34

psychemedia's gravatar image

psychemedia ♦♦
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



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "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



Asked: 14 Jan '11, 15:26

Seen: 1,599 times

Last updated: 12 Jul, 08:08

powered by OSQA