s | p_blank | o_blank |
---|---|---|
/id/statistical-geography/S02001656 | http://www.w3.org/1999/02/22-rdf-syntax-ns#type | geosparql: Geometry |
/id/statistical-geography/S02001656 | geosparql: asWKT | POLYGON ((-3.203483098759254 55.95827262117489, -3.202495754310967 55.95803058687086, -3.2024989525089818 55.95761722371791, -3.1979281552023577 55.95846144591033, -3.198185596598749 55.957982721718786, -3.1976508656019815 55.957411529538966, -3.1966479528527034 55.95757501542202, -3.195418890614841 55.95744308870444, -3.194468775111864 55.95724722785891, -3.193597930206493 55.95672435646609, -3.1935926520502815 55.95603252655427, -3.1922001731667575 55.95554282216782, -3.19235296921242 55.95489899042517, -3.197005134891953 55.9541882817262, -3.198178675738617 55.95384716155426, -3.19996845084354 55.95350479204435, -3.200400841445276 55.95296523400862, -3.201489609597383 55.953608836101424, -3.2001230497655757 55.95382156089778, -3.2003591568288843 55.95420563642208, -3.2013251186200993 55.95436694570586, -3.2034467651746814 55.95409464679007, -3.2093534719509984 55.95296753639342, -3.2090220686865885 55.952311038172475, -3.2094956440443574 55.951295072590796, -3.210579629357985 55.95120332298058, -3.2135405968070967 55.95217158315901, -3.2146646931138596 55.952268331723594, -3.214169789205004 55.95283031094464, -3.2133195553158296 55.95278478304614, -3.212458925716103 55.95343121680841, -3.2121162305899835 55.95425226878575, -3.2112393113691433 55.953865531198716, -3.2098010885474246 55.95448168993538, -3.209705823604838 55.9550217547405, -3.2091696112105885 55.955307122985865, -3.209141414799744 55.95592584390191, -3.2075648416711338 55.95647975210461, -3.207786212298513 55.95741215356192, -3.208500169828477 55.957962854976735, -3.207625041044749 55.958903400592, -3.2070157613063133 55.95813015866733, -3.205883743374039 55.958677101008796, -3.2047202611990966 55.95833715018667, -3.203483098759254 55.95827262117489)) |
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
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
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.
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 |
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).
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
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.
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
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>