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/S02002397 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02002397 geosparql: asWKT POLYGON ((-4.06245699444611 55.782378911906356, -4.060630569600121 55.78127604115522, -4.060046384519382 55.78172732876097, -4.057907766350198 55.78073630187671, -4.05593267460286 55.779673733259976, -4.055120773695156 55.779114780801386, -4.055438135462475 55.778407371447166, -4.0534239496622355 55.778009664976196, -4.053537232635874 55.77773815817021, -4.053548516841304 55.777177649412835, -4.0542709161280355 55.77702009980274, -4.054573117421868 55.77749422275658, -4.0558240402112355 55.77704032747496, -4.054789728503575 55.7767182650641, -4.056634196863255 55.77504435284179, -4.056624567346618 55.77459495066317, -4.057485434643687 55.774832519856275, -4.058169039320711 55.77422484650022, -4.057692843967022 55.773687329884616, -4.058595483543903 55.773267798004355, -4.0588399060774 55.77215828439224, -4.057906039680023 55.77107749809149, -4.05655539569585 55.770255321761425, -4.056830286999621 55.77002604175197, -4.056043769462712 55.76953314923642, -4.05612883144123 55.76881553163963, -4.055429453872145 55.76794649424326, -4.054498418231581 55.7675216968843, -4.055136328151323 55.767223439512335, -4.055606590189011 55.76707177807282, -4.057015624893953 55.765978724779636, -4.05849666675608 55.76564835563652, -4.060591357434872 55.76675264097553, -4.062420864185296 55.76720921742116, -4.062125077035736 55.76764556576889, -4.063114824853198 55.76887816410302, -4.064160223763288 55.76819306621542, -4.065203008166898 55.76841830338963, -4.066624744582569 55.768166940856794, -4.065965644334723 55.76860510454707, -4.0675751636086535 55.769298458362115, -4.0702897168559895 55.77069870143789, -4.07130698474543 55.77034004376349, -4.0716429794929985 55.77096348082477, -4.070750731539291 55.770878278936365, -4.069048539190949 55.77114757783071, -4.068566678274712 55.771466952731814, -4.070022818321234 55.7722154686842, -4.069151360971598 55.772797650833276, -4.069735607029921 55.773141068960356, -4.068139786454152 55.77394039542158, -4.069003594853401 55.77456809331894, -4.0668447318071514 55.77549293273985, -4.067870779300293 55.7763523663842, -4.067521931173164 55.776838609003164, -4.066383187282765 55.77731610000919, -4.065261044674868 55.778107860280315, -4.0636985629327445 55.777810574388795, -4.064050691649058 55.77880738259026, -4.0638990841519895 55.77979336512896, -4.064754050439054 55.78039561910363, -4.063472054616115 55.781669852785704, -4.063528135456445 55.782127264102826, -4.06245699444611 55.782378911906356))
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>