W3C home > Mailing lists > Public > public-linked-json@w3.org > September 2011

Practical J-Triples from SPARQL JSON results

From: Danny Ayers <danny.ayers@gmail.com>
Date: Fri, 2 Sep 2011 00:22:20 +0200
Message-ID: <CAM=Pv=S1QJZOOwPS-e8GPT9XBy72sRN-rCh+f4FxQRYTSjJGEg@mail.gmail.com>
To: public-linked-json@w3.org
Cc: Ivan Herman <ivan@w3.org>
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 -


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.



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

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

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 16:18:32 UTC