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/S02002041 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02002041 geosparql: asWKT POLYGON ((-4.790310394689179 55.94968928301421, -4.786822981407424 55.9492255132426, -4.786930811838359 55.94871970453618, -4.785559521886752 55.94813950151831, -4.785198259589471 55.947347678140666, -4.784342449092476 55.94636029772844, -4.783606555120394 55.94592750094154, -4.782021372988532 55.945808569049056, -4.780586808222922 55.945926249887144, -4.779365282750954 55.94576647790739, -4.7779884644898845 55.94509259263198, -4.779634151100202 55.94512305563598, -4.781987229332257 55.9447761425001, -4.78517603641899 55.94413920915399, -4.788398528907627 55.94298137710162, -4.789699554329847 55.94289523146849, -4.789827826335636 55.94245187710779, -4.790967327518982 55.94253408731395, -4.79185132068573 55.9423206796752, -4.792430911680999 55.94303613602615, -4.794486444159219 55.94264285706382, -4.796169908593094 55.941777670820095, -4.7961562041086925 55.94069932276589, -4.801818544512573 55.94126273796325, -4.802970443427848 55.94058033833039, -4.806048820776281 55.939961899681286, -4.806602046794404 55.94052457151327, -4.805472215604585 55.9408469519304, -4.805712741046877 55.941659530730455, -4.8063993008089625 55.942580154482435, -4.808852222192326 55.94221908493884, -4.8099817977547525 55.94218321522101, -4.810948494848424 55.942435872821555, -4.81176833477935 55.943438737941825, -4.813189857942329 55.94480698139199, -4.8082658822188415 55.945519263198044, -4.80717160910129 55.94436307299907, -4.805975135386456 55.94545993881159, -4.8017339661234635 55.94469394904598, -4.801550961554286 55.945533897462575, -4.800695296588348 55.94589053227907, -4.8011903838401855 55.94642215504334, -4.799975594210454 55.947863016196166, -4.799703405531771 55.94948775652001, -4.799557557931892 55.95157070124763, -4.797726760633157 55.95218821135921, -4.796923904129942 55.95234732142766, -4.795945771924041 55.95196824123524, -4.793828460328146 55.95155344148904, -4.794150531934436 55.95073301262366, -4.79469475382894 55.948658366766544, -4.7939363760323515 55.94859024704716, -4.792406406542588 55.94534172795795, -4.792218217041093 55.945175200813395, -4.789818882820636 55.94463485846409, -4.789107008782809 55.94478001052299, -4.7902590157483695 55.945806112127514, -4.790494927047205 55.94704797012068, -4.789449144110048 55.947051217004834, -4.789310867331557 55.94764454030223, -4.789951562403675 55.94786631146443, -4.789517797686859 55.948993923097056, -4.79014073214494 55.94924481486287, -4.790310394689179 55.94968928301421))
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-staging.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-staging.gov.scot/sparql HTTP/1.1
Host: statistics-staging.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-staging.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-staging.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-staging.gov.scot/sparql

Request the results as JSON, using a GET:

curl -X GET -H "Accept: application/sparql-results+json" http://statistics-staging.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-staging.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>