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/S02002071 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02002071 geosparql: asWKT POLYGON ((-3.0549334970936477 55.8614729688206, -3.056582976872598 55.85937892758582, -3.0559640690782017 55.85894395372829, -3.0536274498316986 55.85879325235651, -3.0538927381141856 55.857380274093046, -3.0556620606716614 55.85744339641408, -3.05541489506974 55.85679627310253, -3.055892905817502 55.856320812984926, -3.05498939435743 55.855403061994, -3.049585571950703 55.85524261408305, -3.052182732998008 55.85440275488848, -3.0533078324571377 55.853476620716854, -3.0534101441554276 55.85312530307897, -3.0517981941788834 55.85226598675613, -3.0510785401708693 55.852048354355574, -3.0513563379474062 55.85147414645946, -3.0509784383270153 55.85067989822855, -3.050049128708689 55.85026510380532, -3.049250844093276 55.85052411839355, -3.0487346214745106 55.850056981374955, -3.0477031195973896 55.85013812093033, -3.0458127096150296 55.84919159395882, -3.0471069165181768 55.848614501755286, -3.047479023285581 55.84879105209847, -3.048497855449764 55.84985163316495, -3.048723596400997 55.849337547170016, -3.0483166433392936 55.848881598269216, -3.0488414805970345 55.84835064009877, -3.0529100404607843 55.84703744365948, -3.053817039328876 55.84691286648923, -3.052176129272979 55.845543501077216, -3.053211493132821 55.84483761177944, -3.0570905972389246 55.84719361156253, -3.0582351187841157 55.84713268028742, -3.0601711782151844 55.84682232986136, -3.0604450086377386 55.847493879978494, -3.063984715798397 55.84727462191814, -3.0647207032790162 55.849658390060874, -3.061097022554883 55.84973462505194, -3.0576292233516247 55.85041538389378, -3.0584976089192937 55.85113889192581, -3.0583493706485565 55.851483510716065, -3.0630206493485166 55.85525306525148, -3.0632819523421286 55.855121596385516, -3.06538412510606 55.855807691838415, -3.065342859813319 55.8566347116879, -3.066704477528612 55.857166525859, -3.0665121688505317 55.857511302830716, -3.065338534484667 55.857442092826325, -3.06500187246497 55.85942290453888, -3.062917574895522 55.85889341690308, -3.0620940219734845 55.85993350538154, -3.06096413078121 55.859674983602034, -3.0604863754556453 55.860358986431756, -3.0585794663387875 55.86040819474679, -3.0581333632238086 55.8615695013242, -3.0590447200847373 55.8617042058903, -3.0586799503298967 55.86288453721821, -3.054356638717428 55.862120614917245, -3.0549334970936477 55.8614729688206))
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>
<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>