s | p_blank | o_blank |
---|---|---|
/id/statistical-geography/S02000863 | http://www.w3.org/1999/02/22-rdf-syntax-ns#type | geosparql: Geometry |
/id/statistical-geography/S02000863 | geosparql: asWKT | POLYGON ((-4.69586464372879 55.7529355684048, -4.690070273125808 55.75350307499893, -4.689579093727577 55.75400821130603, -4.686967295612024 55.75455658727083, -4.686121582476345 55.753772355447694, -4.685300041630475 55.753847683660254, -4.685409189109593 55.75255667386023, -4.686170512554617 55.75241991465368, -4.686009565251177 55.750311101319305, -4.680009890850098 55.74672088654333, -4.680305167691815 55.744521212841136, -4.679454352666243 55.74559145614312, -4.67983347816662 55.74280733428639, -4.678298363758599 55.74319536310782, -4.675881649821955 55.74118728704007, -4.67294425115354 55.741326891678845, -4.670299154047526 55.74259703741977, -4.671788814120311 55.742317559302386, -4.673209466378743 55.74298941801319, -4.672509661477021 55.743997066192485, -4.67400153428015 55.74655812778309, -4.672197869845323 55.74655762036655, -4.669687240661555 55.74521605034645, -4.666075618281041 55.744525387312336, -4.667353378212544 55.74410213508314, -4.666498991076813 55.74279037925197, -4.668409254949232 55.74066351411171, -4.662215535676176 55.73955754187703, -4.6644526169648 55.7380168994118, -4.66360336842304 55.73733239036802, -4.661628140472758 55.7378088981616, -4.661429242824143 55.73705488281814, -4.66235803008344 55.736692178136806, -4.664329282457663 55.73709443746233, -4.663904942357252 55.73751293476493, -4.664800758880309 55.73728127084377, -4.666466558899023 55.73769972555679, -4.666374313853034 55.738209716531955, -4.667637935850464 55.73847794883894, -4.669062691780315 55.73996618177569, -4.670981537368298 55.73960085695057, -4.670839966367518 55.7382286753623, -4.672224614869632 55.73820753982783, -4.672346169690583 55.73718917321427, -4.677139591719599 55.73762406768036, -4.675615740943739 55.738637045836185, -4.676896204597841 55.73869121933301, -4.678088617349645 55.7393831207434, -4.678785928872379 55.738543684072674, -4.680805362970606 55.73917226382213, -4.684014530898534 55.74124478787283, -4.689117141828349 55.74104767614416, -4.692023074961007 55.74182893473976, -4.690256782836816 55.74325190004696, -4.6909582357825 55.743488213992315, -4.690412186526125 55.74412040085111, -4.694528855873161 55.744893044797344, -4.693877566839871 55.74630956470586, -4.695332089889747 55.74683494535171, -4.696039908963207 55.74947107071881, -4.697584067852178 55.74944615134446, -4.696617930168463 55.74976399325044, -4.696599629346674 55.752038540345964, -4.695704305410844 55.752228976548395, -4.69586464372879 55.7529355684048)) |
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>