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/S02001772 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02001772 geosparql: asWKT POLYGON ((-3.3830177143919142 56.139361682928886, -3.383645369005444 56.13891436641662, -3.3823158251911134 56.136844623432346, -3.3833887694793767 56.13714710334635, -3.38427122656457 56.137074319122824, -3.387885617097493 56.13595253993288, -3.3885418829814586 56.135052592105325, -3.3884428305783993 56.13405330724507, -3.38753144028726 56.13376702765828, -3.387063848753481 56.13284677505169, -3.3857766640091516 56.13195368965886, -3.3850785147724394 56.131107918000495, -3.3861469747980655 56.13095829226446, -3.3886866528070354 56.13020548062227, -3.3899735494083654 56.12973742667475, -3.389212349298546 56.1284884447048, -3.389985197356883 56.128343915858466, -3.390836664175626 56.12988704112409, -3.3915385513976593 56.13026505321129, -3.393405769088954 56.129854822865916, -3.3952961768772147 56.131792607042996, -3.3967482564470517 56.13412140884811, -3.3975001236448934 56.136065182247705, -3.3974286508220106 56.13653549165162, -3.3964377205807623 56.13753940563679, -3.3960104329243115 56.1381822047353, -3.394639209432692 56.13808986779505, -3.394104637752311 56.13843735032443, -3.391724846988419 56.13849114149023, -3.387929953095017 56.13886179890432, -3.3888078159656647 56.139917816148696, -3.3863942198388792 56.14061774152429, -3.386182896984668 56.140806058264495, -3.386698046081097 56.14159495131923, -3.3873834607975675 56.14143053992656, -3.387844887914121 56.14185907642672, -3.387697361577107 56.14239121774386, -3.3869650471278376 56.142759492116994, -3.383548532590115 56.14320690810967, -3.3829081784629302 56.14348184475677, -3.3813961007413846 56.14305667760975, -3.3808018644649867 56.14304714715651, -3.379851153602733 56.14373437456241, -3.3797748653738062 56.14407577532676, -3.3781082416990675 56.14459764255283, -3.3754360230370444 56.14327989504695, -3.3765095426250955 56.14341714143116, -3.378014809885939 56.14384248901525, -3.3799951364523504 56.14314089437899, -3.3810092841766255 56.14140027419784, -3.3840315734477113 56.140526516196275, -3.3830177143919142 56.139361682928886))
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>