s | p_blank | o_blank |
---|---|---|
/id/statistical-geography/S02002176 | http://www.w3.org/1999/02/22-rdf-syntax-ns#type | geosparql: Geometry |
/id/statistical-geography/S02002176 | geosparql: asWKT | POLYGON ((-3.989389663034185 55.856146347971965, -3.989274109219557 55.85555049851354, -3.9919058936040743 55.85548321555075, -3.9944658637933492 55.854929483127606, -3.9961491401032507 55.8546745685039, -3.9983395587221047 55.85522599366767, -3.99873625793495 55.855424737839755, -3.999686431304646 55.85702854843064, -4.000743722946559 55.85770334938917, -4.007320727541086 55.856757437940644, -4.008561890411398 55.85665910042779, -4.013753315939598 55.856625092677575, -4.018412129988318 55.856399196909074, -4.0183916391715675 55.85600410733054, -4.016345440516939 55.85568169105436, -4.016293940896732 55.85468147918746, -4.014426256088364 55.853866892210156, -4.014058922730616 55.853172338878124, -4.01478532107864 55.85300766514793, -4.017787304715974 55.85382918624026, -4.0180565276235 55.85354718078649, -4.020112322732607 55.854693864559955, -4.019970115359615 55.8550165937483, -4.027327010689412 55.85975751555169, -4.0289989534374815 55.86117685265908, -4.029801416897388 55.862163513899965, -4.029375412338009 55.86226705292054, -4.026960643612338 55.86254052353365, -4.025031076741446 55.86231167829195, -4.022908719975131 55.861447899624594, -4.021837676078878 55.861132989324766, -4.016016086086818 55.86015534311995, -4.015777665913438 55.860697792329454, -4.017935845569674 55.860849626531355, -4.018824144181595 55.860798817184346, -4.019194426454065 55.861440860703574, -4.021338957033896 55.861486984435295, -4.021199529321554 55.86238367427822, -4.022347575016614 55.86244452167717, -4.022046610720944 55.86335521833332, -4.022180937455459 55.86404810596229, -4.015112145242696 55.86394842262065, -4.009381098379367 55.86373604719676, -4.0089139109701435 55.86337575222999, -4.0093963116984925 55.862505129558734, -4.00884297755708 55.86200191143589, -4.0056578646162455 55.86146513729527, -4.005641080136814 55.86097179551336, -4.004816135240255 55.86076449618439, -4.004844649862305 55.86192826842578, -4.003985622131565 55.86180231118384, -4.003020487100807 55.86193420258982, -4.002986074019548 55.86039091924897, -4.002066863899646 55.860241430456426, -4.002302186571658 55.85954166307844, -4.001230677834031 55.85940629399852, -4.00136628679342 55.858784503281846, -3.999012496052659 55.858582068546646, -3.9993219106147473 55.85770849154105, -3.997506629698995 55.85771081324504, -3.9955521571935266 55.85741921179744, -3.9923468226693695 55.85641233894941, -3.991317556466884 55.85620455681381, -3.989389663034185 55.856146347971965)) |
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
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
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.
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-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.
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
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>