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/S02002331 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02002331 geosparql: asWKT MULTIPOLYGON (((-4.857192188991577 55.2473684566623, -4.857888996577597 55.24642565635849, -4.85757507545288 55.24534076358392, -4.856312664107053 55.244545083378114, -4.856324959225944 55.24388315340096, -4.8573726911886865 55.244033907950374, -4.860066605316735 55.244802305995314, -4.8604306691797925 55.24551114615918, -4.859986500500908 55.24717232574798, -4.85860603161268 55.24714151198688, -4.857753675745161 55.24842004629556, -4.8582375428679665 55.24946050213435, -4.857597283372179 55.249529443874614, -4.85750593644981 55.248695542184485, -4.856619729504477 55.24862640617181, -4.857192188991577 55.2473684566623)), ((-4.843715902671031 55.238715708300774, -4.844826221068614 55.23752115243098, -4.846444590197196 55.237932869668505, -4.847758456913977 55.23804602811409, -4.852646596418304 55.2382014650791, -4.855186077974689 55.23785436008659, -4.855315371828191 55.23704143205864, -4.854511551807159 55.23612614942122, -4.852981522264141 55.23519106953851, -4.853711556185378 55.23484136739617, -4.855038066959773 55.23579019328582, -4.855289100863275 55.234903323890684, -4.855260924279623 55.23385218770875, -4.855999020365399 55.232738156902016, -4.855261994439178 55.23271660940018, -4.855500495739876 55.23169803646016, -4.856655066869276 55.231672007727234, -4.856840180545808 55.23240381150582, -4.8583056550393415 55.232295517245745, -4.858892707033043 55.23360527476797, -4.859362701807851 55.23356794933131, -4.860286557136189 55.235643818067274, -4.861075004820866 55.23810019125092, -4.861751191615058 55.23967190891155, -4.862092610548564 55.241379134774974, -4.862940384412492 55.243716339174945, -4.863095178489407 55.24488406550771, -4.861746642807876 55.24468648051213, -4.860201674578683 55.24427324007415, -4.860203257843527 55.2438354039692, -4.858050285493577 55.24345617287606, -4.856435732127289 55.243519172787146, -4.855474488075101 55.24417905407949, -4.855960413961809 55.24484624011514, -4.856261798665417 55.24517376479656, -4.853601076339124 55.24675087017526, -4.852141748700224 55.2473137455299, -4.849789627425927 55.24743912950076, -4.8487497620840845 55.247722740235396, -4.847967528513153 55.24822645214718, -4.846939155373248 55.250129294595204, -4.845937232517737 55.250862871111416, -4.844406807721677 55.250960817946165, -4.844551880191909 55.24948021721578, -4.845025192174871 55.24836317839801, -4.845782877441502 55.24804643248378, -4.8486968357245885 55.24720312364833, -4.848205204088545 55.24627014208588, -4.847128554325207 55.24529742839297, -4.844587207476799 55.24386444771925, -4.843427684863444 55.24283070829577, -4.842391666446592 55.241101875524485, -4.84211094016639 55.2393734218611, -4.8431740354766655 55.239713161978784, -4.843845397693192 55.23920712038644, -4.843715902671031 55.238715708300774)))
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>