With SPARQL queries such as this one, you get results that look like this:
{
"head": {
"vars": [
"author",
"authorLabel",
"authorDescription",
"authorAltLabel",
"birth"
]
},
"results": {
"bindings": [
{
"author": {
"type": "uri",
"value": "http://www.wikidata.org/entity/Q184226"
},
"birth": {
"datatype": "http://www.w3.org/2001/XMLSchema#dateTime",
"type": "literal",
"value": "1925-01-18T00:00:00Z"
},
"authorLabel": {
"xml:lang": "en",
"type": "literal",
"value": "Gilles Deleuze"
},
"authorDescription": {
"xml:lang": "en",
"type": "literal",
"value": "French philosopher"
},
"authorAltLabel": {
"xml:lang": "en",
"type": "literal",
"value": "Deleuze, G. Deleuze, Zhilʹ Delëz"
}
}
]
}
}
simplifySparqlResults
converts it to a way simpler:
[
{
"author": {
"value": "Q184226",
"label": "Gilles Deleuze",
"description": "French philosopher",
"aliases": "Deleuze, G. Deleuze, Zhilʹ Delëz"
},
"birth": "1925-01-18T00:00:00Z"
}
]
That's still hairy, because we requested many variables, but that can get even simpler if there is only one variable!
Say instead of "vars" : [ "author", "authorLabel", "birth" ]
, we only ask for "vars" : [ "author" ]
:
import { simplifySparqlResults, minimizeSimplifiedSparqlResults } from 'wikibase-sdk'
simplifySparqlResults(results)
// => [ { "author": "Q3731207" } ]
minimizeSimplifiedSparqlResults(simplifySparqlResults(results))
// => [ "Q3731207" ]
And then to make it even more simpler, we can... hum no, that's all we got.
Use it like so:
const simplifiedResults = simplifySparqlResults(results)
or for a more complete example:
// see the "SPARQL Query" section above
const url = wbk.sparqlQuery(SPARQL)
const rawResults = await fetch(url).then(res => res.json())
const simplifiedResults = simplifySparqlResults(rawResults)
When only one variable is requested, you can use minimizeSimplifiedSparqlResults
: the results will consist of an array of this variable values, instead of an array of objects.
simplifySparqlResults(results)
// => [ { item: "Q112983" }, { item: "Q185598" }, { item: "Q3879286" } ]
minimizeSimplifiedSparqlResults(simplifySparqlResults(results))
// => [ "Q112983", "Q185598", "Q3879286" ]