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/S02002429 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02002429 geosparql: asWKT POLYGON ((-4.201023061355371 55.74352192310754, -4.200081210850485 55.74320626234822, -4.199123732744299 55.742405631381224, -4.193661617254589 55.74199504014909, -4.190509916821064 55.74180458096211, -4.19154982453056 55.74079737131257, -4.199193970651239 55.738198151380836, -4.202790920904592 55.73705860895655, -4.209025022517526 55.73457049807013, -4.21196223340588 55.73355596046492, -4.212303846493318 55.733200677209425, -4.2123695103429055 55.732506700178796, -4.213178402926359 55.73252172862285, -4.213607983960922 55.73417788122829, -4.212511288235683 55.73447016559858, -4.21262845223206 55.73499461524642, -4.211616125740679 55.735454264455214, -4.211224026794801 55.73605545415648, -4.213883340967272 55.73717816405557, -4.215794493377433 55.73789892509442, -4.216716436474044 55.73839977012338, -4.214540069465042 55.73887710582137, -4.213572676827877 55.73891802244793, -4.212102469482822 55.73870990435768, -4.2094123352926 55.73816005254789, -4.207904815823813 55.73818897223112, -4.206087185201201 55.73860495506511, -4.207265220651846 55.73899249351849, -4.207290270632749 55.74017545440916, -4.209676377271774 55.74017597176568, -4.211495583424851 55.74055726166548, -4.212627585253417 55.740527272880904, -4.211923785970343 55.74132186453636, -4.213991823362982 55.74211145056376, -4.214354321990975 55.742042002389326, -4.218065349363084 55.74286479814868, -4.218286682547138 55.74395727793163, -4.218512731964953 55.74485194666747, -4.217818200510973 55.745486786337594, -4.215771876725411 55.74577148976142, -4.215075244296351 55.746095706878386, -4.214001420043524 55.74693934944017, -4.213541131612493 55.746893658097065, -4.212853678637843 55.74742621580275, -4.2120037534855825 55.747752107180716, -4.211524800949806 55.747503465088606, -4.211849074006848 55.74703926314786, -4.210029399930544 55.74613732058044, -4.208269171931197 55.74628581978073, -4.208086660155916 55.745875676504156, -4.208716280403813 55.74502709795143, -4.203676024897429 55.743384473800035, -4.202717555199388 55.743904967427554, -4.201023061355371 55.74352192310754))
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>