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/S02002070 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02002070 geosparql: asWKT POLYGON ((-3.0472474762135064 55.871651966936255, -3.0473995294057143 55.87077010155386, -3.0480064425096893 55.86956987313151, -3.049390092867766 55.86813838434182, -3.0481798148342985 55.86782356152947, -3.0486292296685997 55.86718185709443, -3.0491720890419582 55.867352454735126, -3.0515141823870744 55.86454405011529, -3.0520750200400784 55.86460215434255, -3.0539348319144173 55.86247466076076, -3.0532494677102053 55.86204493297517, -3.0539933846146035 55.86127393777819, -3.0549334970936477 55.8614729688206, -3.054356638717428 55.862120614917245, -3.0586799503298967 55.86288453721821, -3.0590447200847373 55.8617042058903, -3.0581333632238086 55.8615695013242, -3.0585794663387875 55.86040819474679, -3.0604863754556453 55.860358986431756, -3.06096413078121 55.859674983602034, -3.0620940219734845 55.85993350538154, -3.0642810822081445 55.8604537348452, -3.0639534947928313 55.86132815696379, -3.0644184523239804 55.862332402392, -3.065422106783368 55.86221038268069, -3.0661267225883995 55.86367252972716, -3.0672553406957395 55.86442033650748, -3.070694721350923 55.866317819897965, -3.0691257844919124 55.866822462231895, -3.069961018952992 55.86860752165712, -3.0689537613197015 55.86859807985807, -3.067803741298477 55.86888751162178, -3.067774598282992 55.87015471239692, -3.066191877825223 55.87013249339033, -3.065808220369816 55.87129493883079, -3.067934226294067 55.871312449579804, -3.0678352054953204 55.871780551888754, -3.0671510870620344 55.87203243566557, -3.064750506230475 55.871771337688024, -3.0622500267299992 55.87363596716949, -3.0607775528854035 55.87318041910892, -3.0585840907849593 55.87479871278191, -3.05717974968936 55.87428962948972, -3.056844551523546 55.87371744106485, -3.0553835626904005 55.87288534853482, -3.055029218299138 55.87337359987706, -3.054020624670125 55.87331035885168, -3.0542781376006416 55.87278699833084, -3.0556041120835493 55.87158954617046, -3.0543483219300875 55.87073465532897, -3.053594479512635 55.87175270017703, -3.0517679191927405 55.87165411074987, -3.051731914822359 55.8714254158905, -3.049944352676191 55.87141798273959, -3.0472474762135064 55.871651966936255))
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>