add cart choropleth column cross cube error file folder geo help home lock obs poi rdf remove search slice spreadsheet success table unlock warning

[this is a icon-] developer tool

SPARQL 1.1 Query: Results

Edit query
Query results
s p_blank o_blank
http://statistics.gov.scot/id/statistical-geography/S02001513 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02001513 geosparql: asWKT POLYGON ((-4.343552129212089 55.926291904703, -4.339222051116967 55.92509801489678, -4.338092981713809 55.92362752492684, -4.336922974037249 55.922804907409855, -4.336788671897534 55.922169339089066, -4.336004720014941 55.9221752200876, -4.335166975693242 55.92155300464322, -4.3334875204899515 55.91917618115173, -4.334113290870963 55.91867000192207, -4.3334415954546595 55.91789211704547, -4.332159274682494 55.917956402113, -4.331329611847791 55.91735663934315, -4.330935428314571 55.91686431422603, -4.3320057983635 55.916810033528385, -4.334402207746552 55.91602217767335, -4.33528001381305 55.91491804093861, -4.336792960409714 55.91452981806532, -4.3394737819059275 55.9146676383707, -4.343500061456975 55.91323392709159, -4.343237544298717 55.9128614454475, -4.34348353846397 55.91216471738213, -4.345268807264708 55.9120805477582, -4.347560645638827 55.91142964926002, -4.348501708303847 55.91098820924766, -4.347600927351922 55.911927181089666, -4.348114588175684 55.91240764980837, -4.348411691038065 55.91268554926334, -4.3475200924614485 55.913845779559615, -4.347920456698963 55.91403587120918, -4.348688053444629 55.915164451904694, -4.348104599249739 55.915837070351806, -4.34623046670811 55.9164974425577, -4.347186830732866 55.916724572940396, -4.3480705089499 55.91670232825246, -4.348951475693148 55.91705850719563, -4.350467424640958 55.91724090654468, -4.3499247621633685 55.91732663669056, -4.349516537658533 55.91985734848377, -4.34982497203553 55.920344575582426, -4.349259806884101 55.92057926992672, -4.349121437681259 55.92124258894804, -4.352477163454016 55.92115154852157, -4.352524790989839 55.92088101010306, -4.355687707440355 55.921305860213515, -4.354994576079785 55.92228079597763, -4.360711415316509 55.92337563974341, -4.359769944942108 55.925793393278894, -4.358478822483277 55.92562041443372, -4.357241643067318 55.925545235167164, -4.353849218407337 55.925565202408514, -4.351981202814618 55.925645840127245, -4.351707539829211 55.92588474538852, -4.347129989966095 55.92616083594093, -4.345267896394518 55.92634012187077, -4.343552129212089 55.926291904703))
SPARQL API: The Basics

The most flexible way to access the data is by using SPARQL, a query language, analagous to SQL for relational databases, for retrieving and manipulating data from graph databases like ours. We support SPARQL 1.1 query syntax. Many online tutorials are available.

To submit a SPARQL query from your code, you issue an HTTP GET or POST to our endpoint:http://statistics.gov.scot/sparql, with the query itself as a url-encoded parameter called query.

For example, to run the following simple SPARQL query and get the results as JSON:

SELECT * WHERE {?s ?p ?o} LIMIT 10

Option 1: POST (recommended)

Issue a POST to the endpoint, with the query in the body, and an Accept header of sparql-results+json:

POST http://statistics.gov.scot/sparql HTTP/1.1
Host: statistics.gov.scot
Accept: application/sparql-results+json
Content-Type: application/x-www-form-urlencoded

query=SELECT+%2A+WHERE+%7B%3Fs+%3Fp+%3Fo%7D+LIMIT+10

Option 2: GET

Issue a GET to the following URL (note the .json extension - see the formats section for more detail on this):

GET http://statistics.gov.scot/sparql.json?query=SELECT+%2A+WHERE+%7B%3Fs+%3Fp+%3Fo%7D+LIMIT+10

Scroll down to the end of this page for examples of both of these methods in a few different languages.

Results formats

As with other aspects of our API, to get the data in different formats, you can use either (a) a format extension or (b) an HTTP Accept header. Available result formats depend on the type of SPARQL query. There are four main forms:

SELECT queries return tabular results, and the formats available reflect this:

Format Extensions Accept Headers
XML .xml application/xml,
application/sparql-results+xml
JSON .json application/json,
application/sparql-results+json
Text .txt, .text text/plain
CSV .csv text/csv

CONSTRUCT and DESCRIBE queries return graph data, so the results are available in the same formats as our resource APIs:

Format Extensions Accept Headers
RDF/XML .rdf application/rdf+xml
N-triples .nt, .txt, .text application/n-triples,
text/plain
Turtle .ttl text/turtle
JSON-LD .json application/ld+json,
application/json

ASK queries return a boolean result:

Format Extensions Accept Headers
XML .xml application/xml,
application/sparql-results+xml
JSON .json application/json,
application/sparql-results+json
Text .txt, .text text/plain
Results pagination

We accept page and per_page parameters for paginating the results of SELECT queries (we automatically modify your query to apply LIMIT and OFFSET clauses). For other query types (i.e. DESCRIBE, CONSTRUCT, ASK), pagination like this doesn’t make so much sense, so these parameters are ignored.

For requests made through the website (i.e. HTML format), the page size is defaulted to 20. For requests to our sparql endpoint for data formats (i.e. non-HTML), there will be no defaults for these parameters (i.e. results are unlimited. For performance reasons we generally advise LIMITing your query if possible).

Parameter Substitution

You can parameterise your SPARQL by including %{tokens} in your queries, and providing values for the tokens in the request parameters.

Note that the following tokens are reserved and cannot be used as parameters for substitution:

  • controller
  • action
  • page
  • per_page
  • id
  • commit
  • utf8
  • query
Cross Origin Resource Sharing

Our servers are configured to allow access from all domains. This means that if you’re writing JavaScript to request data from our server in to a web page hosted on another domain, your browser should check this header and allow it.

If you need to support very old browsers, you can additionally pass a callback parameter and the results will be wrapped in that function. For example:

http://statistics.gov.scot/sparql.json?callback=myCallbackFunction&query=SELECT+%2A+WHERE+%7B%3Fs+%3Fp+%3Fo%7D+LIMIT+10

This help topic on the jQuery website has more details.

Examples

Using cURL

Here’s a couple of examples running a query using the widely available cURL command line program.

Request the results as XML, using a POST:

curl -X POST -H "Accept: application/sparql-results+xml" -d "query=SELECT%20*%20WHERE%20%7B%3Fs%20%3Fp%20%3Fo%7D%20LIMIT%2010" http://statistics.gov.scot/sparql

Request the results as JSON, using a GET:

curl -X GET -H "Accept: application/sparql-results+json" http://statistics.gov.scot/sparql?query=SELECT%20*%20WHERE%20%7B%3Fs%20%3Fp%20%3Fo%7D%20LIMIT%2010

Using JavaScript

This example HTML page uses jQuery to issue a POST to our SPARQL endpoint, requesting the results as JSON.

<!DOCTYPE html>
<html lang='en'>
<head>
	<script src='http://code.jquery.com/jquery-1.9.1.min.js'></script>
</head>
<body>
<script type='text/javascript'>

	var query = 'SELECT * WHERE {?s ?p ?o} LIMIT 10';
	var url = 'http://statistics.gov.scot/sparql.json';
	$.ajax({
		method: 'POST',
		dataType: 'json',
		url: url,
		data: {query: query},
		success: function(data) {
			alert('success: ' + data.results.bindings.length + ' results');
			console.log(data);
		}
	});
</script>
</body>
</html>