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/S02001010 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02001010 geosparql: asWKT POLYGON ((-4.43152783556443 55.87943998017799, -4.424384160394492 55.88026402156155, -4.419067815821369 55.88219319910134, -4.414324829618071 55.880488792785506, -4.410690914072554 55.88067688441892, -4.410713431672282 55.87975070870554, -4.409592649180107 55.88127361703773, -4.407349765715498 55.88249492554519, -4.410170679397632 55.87924902836443, -4.410001397278991 55.875486461311866, -4.411420779852818 55.87285218569776, -4.418091996624824 55.870618084503995, -4.420902946248254 55.8680102507834, -4.423012392546382 55.8638972220901, -4.423736814674762 55.86167194172437, -4.422951596739216 55.859071943078646, -4.424019213129878 55.85671406557718, -4.426482436853378 55.85444549126791, -4.423408370407193 55.85280739713279, -4.42201827762932 55.85101024574573, -4.422914030815428 55.849743290208096, -4.422445950450943 55.8476133975714, -4.422535774735557 55.850083285590884, -4.421818976663086 55.85088833953323, -4.421502524156367 55.84965424189976, -4.419099460226194 55.8495487458002, -4.418423201918316 55.8515213923419, -4.416358424662426 55.85069016095436, -4.413873845085218 55.85055918762911, -4.410082907657956 55.85126268993474, -4.416577944213033 55.8485647118634, -4.416898066762055 55.84703048294135, -4.41563471369316 55.846643398917216, -4.415329300197169 55.84620746136446, -4.416322974626776 55.84548688747729, -4.417034780789444 55.84665929389736, -4.419173206198577 55.8463655933492, -4.419244403802749 55.84725398483886, -4.420177908703895 55.84762209374537, -4.426326420708004 55.847330176822375, -4.426264526310647 55.84813131824714, -4.426825548041139 55.84739222676632, -4.430629850165714 55.8474249514858, -4.431039141447958 55.847839288569084, -4.430360346096109 55.848481862564824, -4.431077610959141 55.849222666663294, -4.432055602110615 55.84860114763761, -4.439279701079285 55.85084884932288, -4.442861854515005 55.85039126974109, -4.444183620323773 55.85261198205541, -4.4459233885553 55.852074075315194, -4.447287556769484 55.85242443384442, -4.447610368425642 55.852984244640595, -4.452520170581889 55.85330886445063, -4.450045830028881 55.85486815743753, -4.450291467885305 55.85597776978103, -4.454655742584691 55.85879384326867, -4.460371640159893 55.86401847636101, -4.454729638757641 55.86351103948148, -4.447957513912105 55.86662997863525, -4.446133969272663 55.86889525875617, -4.448567268028267 55.87225326348787, -4.430916679324755 55.87580298291608, -4.430242551768375 55.87755096050407, -4.43152783556443 55.87943998017799))
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>