Published
Edited
Jul 24, 2020
Importers
Insert cell
Insert cell
Insert cell
Insert cell
function parseSPARQL(response, prefixes) {
//Init a new array
let query_results_extract = [];

//Loop through response bindings
for (let i = 0; i < response.results.bindings.length; i++) {
// For each response row,
let newObject = {};
let row = response.results.bindings[i];

// Store each variable and its value into an object key/value pair
for (let variable of response.head.vars) {
// Check for valid data
if (row !== undefined && row[variable] !== undefined && row[variable]) {
//If numerical value
if (!isNaN(row[variable].value)) {
newObject[variable] = +row[variable].value;
}
//If literal, replace prefixes
else {
newObject[variable] = replaceMap(row[variable].value, prefixes);
}
// Store new object in the array
query_results_extract[i] = newObject;
}
}
}

function replaceMap(replaceString, map) {
for (let key of map.keys()) {
replaceString = replaceString.replace(key, map.get(key));
}
return replaceString;
}
return query_results_extract;
}
Insert cell
Insert cell
function parseSPARQLPrefixes(SPARQL_query) {
return new Map(
SPARQL_query.substr(0, SPARQL_query.indexOf('SELECT'))
.split("\n")
.filter(line => line.startsWith("PREFIX"))
.map(row => {
let rowArray = row.split(' ');
let URIs = rowArray
.pop()
.replace("<", "")
.replace(">", "");
let prefixes = rowArray.pop();
let newRow = [URIs, prefixes];
return newRow;
})
);
}
Insert cell
Insert cell
prefixes = new Map([
['http://www.w3.org/2001/XMLSchema#', 'xsd:'],
['http://purl.org/vocab/relationship/', 'ref:'],
['http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'rdf:'],
['http://www.w3.org/2000/01/rdf-schema#', 'rdfs:'],
['http://semantic-mediawiki.org/swivt/1.0#', 'swivt:']
])
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more