s | p_blank | o_blank |
---|---|---|
/id/statistical-geography/S01007753 | http://www.w3.org/1999/02/22-rdf-syntax-ns#type | geosparql: Geometry |
/id/statistical-geography/S01007753 | geosparql: asWKT | POLYGON ((-2.8814853344268 56.47898953832191, -2.8854821908512065 56.478086347288674, -2.887098520690245 56.477778322980086, -2.8873002432436565 56.478073366526374, -2.888837474950772 56.47773052223291, -2.8887279066453475 56.47733541803793, -2.891798752304099 56.47674736996927, -2.901037312855643 56.475422850879866, -2.900943351333707 56.47655266706321, -2.9004072327615615 56.4772283661176, -2.901261065194333 56.477798610315425, -2.9025751303488865 56.47797223783101, -2.9025509823944122 56.47832280548757, -2.9006152711764286 56.47816615931489, -2.9001500601637504 56.478403127972534, -2.899349565501328 56.478193303717205, -2.8973238999191175 56.47835172118882, -2.896639510614103 56.477557044797535, -2.8943098910353116 56.47790627167261, -2.8924408005934406 56.47836864568047, -2.8925245531099457 56.479311702298006, -2.890303126903996 56.47955813846355, -2.8899253432197543 56.47994130041378, -2.8901106014088356 56.480919271646876, -2.8861429746688763 56.48130764112068, -2.8840424299086616 56.48205508653694, -2.8827531916991394 56.48281933488028, -2.8820084475829275 56.48227590643925, -2.881011082317109 56.48096398101645, -2.8825925227200933 56.48059122571382, -2.8814853344268 56.47898953832191)) |
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>