skip to Main Content

given this RDF:

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
@prefix ex: <http://example.com/>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.

ex:bob ex:has '{"name": robert"}'^^rdf:JSON .
ex:bob ex:at "204453"^^xsd:dateTime .

an RDF engine (such as Apache Jena) can validate some of the typed literals (e.g. xsd:dateTime):

$ ~/Downloads/apache-jena-4.7.0/bin/riot --validate file.ttl 
11:48:47 WARN  riot            :: [line: 6, col: 14] Lexical form '204453' not valid for datatype XSD dateTime

will any RDF engines validate the rdf:JSON typed literal?

notice the json isn’t well-formed.

2

Answers


  1. Chosen as BEST ANSWER

    I like John Walker's solution but we also discovered that if you attempt to serialize that RDF (with the malformed json literal) as jsonld using Apache Jena it will validate the json literals:

    $ apache-jena-4.7.0/bin/riot --formatted=jsonld file.ttl
    
    org.apache.jena.shared.JenaException: Exception while writing JSON-LD 1.1
    ...
    Caused by: JsonLdError[code=An invalid JSON literal was detected [code=INVALID_JSON_LITERAL]., message=An invalid JSON literal was detected [code=INVALID_JSON_LITERAL].]
    

  2. You can use JavaScript SPARQL Functions in ARQ to implement a simple custom function that will tell you if a literal is parsable as JSON.

    This Gist demonstrates how that can be done: https://gist.github.com/jaw111/3955e6fb9aa48c7d627cec685a9d5cca

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search