Skip to content

Instantly share code, notes, and snippets.

@jaw111
Last active September 27, 2023 10:00
Show Gist options
  • Save jaw111/3955e6fb9aa48c7d627cec685a9d5cca to your computer and use it in GitHub Desktop.
Save jaw111/3955e6fb9aa48c7d627cec685a9d5cca to your computer and use it in GitHub Desktop.
Validate rdf:JSON typed literal
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix ex: <http://example.com/> .
# valid JSON literals
ex:bob ex:has '{"name": robert"}'^^rdf:JSON .
ex:bob ex:has '{"name": ["robert", "bob"]}'^^rdf:JSON .
# invalid JSON literals
ex:bob ex:has '{"name": "robert"}'^^rdf:JSON .
ex:bob ex:has '{"name": ["robert", "bob",]}'^^rdf:JSON .
PREFIX js: <http://jena.apache.org/ARQ/jsFunction#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?o (js:isJsonLiteralValid(?o) AS ?isValid)
WHERE {
?s ?p ?o .
FILTER (datatype(?o) = rdf:JSON)
}
// validate json
function isJsonLiteralValid(jsonLiteral) {
try {
JSON.parse(jsonLiteral);
return true;
} catch(e) {
return false;
}
}
#!/bin/bash
export JVM_ARGS=-Djena:scripting=true
arq --set arq:js-library=myjs.js --query jsontest.rq --data json-literals.ttl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment