Practical J-Triples from SPARQL JSON results

Ivan was talking earlier about the potential for a "J-Triples" syntax,
a JSON version of NTriples.

There's a W3C Working Group Note on Serializing SPARQL Query Results
in JSON [1]. The serialization is a fairly direct translation of the
XML results format into JSON. It's very easy to derive a practical
J-Triples format from it.

If we run the query:

SELECT ?s ?p ?o WHERE { ?s ?p ?o }

over an arbitrary graph, we get something like:

{
  "head": {
    "vars": [ "s" , "p" , "o" ]
  } ,
  "results": {

    "bindings": [
      {
        "s": { "type": "uri" , "value": "http://hyperdata.org/seki/Hello" } ,
        "p": { "type": "uri" , "value":
"http://purl.org/dc/elements/1.1/date" } ,
        "o": { "type": "literal" , "value": "2011-08-30T19:00Z" }
      } ,
      {
        "s": { "type": "uri" , "value": "http://hyperdata.org/seki/Hello" } ,
        "p": { "type": "uri" , "value": "http://xmlns.com/foaf/0.1/maker" } ,
        "o": { "type": "uri" , "value": "http://dannyayers.com/me#" }
      } ,
...

If we stick such a result set into a JSON/Javascript object -

var sr = // results //

it's possible to access the values through paths like -

sr.results.bindings[0].o.value

which in the above is "2011-08-30T19:00Z"

but there's stuff here that's redundant, so let:

var jt = sr.results.bindings;

effectively making jt an array of triples, so now, e.g.:

jt[0].o.value == "2011-08-30T19:00Z"
jt[0].o.type == "literal"

There's still a bit of redundancy because :

s.type can only be "uri" | "bnode"
p.type can only be "uri"
though o.type can be "uri" | "bnode" | "literal"

but if you wanted to serialize without using SPARQL, you could just
leave the redundant pairs out.

There's also xml:lang and xsd typed literals to consider, but again
those are effectively already spec'd out in SPARQL results, e.g. :

        "o": { "type": "literal" , "value": "2011-08-30T19:00Z",
"datatype": "http://www.w3.org/2001/XMLSchema#integer"}

        "o": { "type": "literal" , "value": "hello", "xml:lang":"en" }

The only question for which there wasn't already an answer was whether
to use "s" or "subject" for the node labels. I've chosen the shorter
form here on the basis that it's fewer bits and anyone using a format
like this will recognize the s, p, o pattern.

Thoughts?

Cheers,
Danny.

[1] http://www.w3.org/TR/rdf-sparql-json-res/


-- 
http://dannyayers.com

Received on Thursday, 1 September 2011 22:22:58 UTC