RDF/JSON

Hi all,
as a follow up to the discussion we started on today's call I'd like to 
share the following write-up one of my colleagues provided to explain why 
they want to be able to use RDF/JSON as well as JSON-LD. His argument is 
based on very practical experience programming with both formats.

Gregg, I don't mean to ignore the response you sent me offline, maybe you 
can repost it here so that everyone can participate in the discussion.

-----
We like JSON-LD. One use-case we have for it is to store RDF data in a 
JSON database like MongoDB or CouchDB. We store in the database the RDF 
data serialized in JSON-LD.

Although JSON-LD is working well for us in the database, it turns out to 
be very clumsy to work with in the programming languages we use, even in 
Javascript where there should be a natural fit. From a programming point 
of view we have found that RDF-JSON is much more convenient than JSON-LD. 
Here is an example to illustrate why.

We have many documents that look like the following, in Turtle format. 
Assume this is the representation of the resource 
http://acme.com/resourceX


@prefix dc: <http://purl.org/dc/terms/>.
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
<http://acme.com/resourceX>
        dc:title "resource X";
        … other triples here …
        dc:description "description of resource X".
<http://acme.com/collectionM> rdfs:member <http://acme.com/resourceX>.

We have lots of these, for varying values of X and M (more Xs than Ms 
generally). 

My programming task is to extract the triples of resourceX if resourceX is 
an rdfs:member of CollectionM. 

Let's consider first an RDF-JSON representation of the above Turtle:

{"http://acme.com/resourceX" :
    {"http://purl.org/dc/terms/title":"resource X",
    "http://purl.org/dc/terms/description":"description of resource X",
    ... other predicates ...
    },
 "http://acme.com/collectionM" : 
    {"http://www.w3.org/2000/01/rdf-schema#member" : {"@id" : "
http://acme.com/resourceX"}}
}

Here is the Javascript code to perform my programming task:

result = {};
if ('http://acme.com/collectionM' in representation) {
    subject = 
representation['http://acme.com/collectionM']['http://www.w3.org/2000/01/rdf-schema#member']['@id'];
    for (var predicate in representation[subject]) {
        result[predicate] = representation[subject][predicate];
        }
    }

Now here is the same resource in JSON-LD format:

[
    {"@id" : "http://acme.com/resourceX",
    "http://purl.org/dc/terms/title":"resource X",
    "http://purl.org/dc/terms/description":"description of resource X",
    ... other predicates ...
    },
    {"@id": "http://acme.com/collectionM",
    "http://www.w3.org/2000/01/rdf-schema#member" : {"@id" : "
http://acme.com/resourceX"}
    }
]


I could have made this array the value of an '@graph' property – it would 
not have changed the example much. Here is the corresponding Javascript:

result = {};
for (var collectionNode in representation) {
    if (collectionNode['@id'] == 'http://acme.com/collectionM') {
        subject = 
subjectNode['http://www.w3.org/2000/01/rdf-schema#member']['@id'];
        for (var subjectNode in representation) {
            if (subjectNode['@id'] == subject) {
                for (var predicate in subjectNode) {
                    if (predicate != '@id') { // '@id' is not a predicate
                        result[predicate] = subjectNode[predicate];
                        }
                    }
                break;
                }
            } 
        break;
        }
    }

As you can see, this is much more complicated than what I have to write 
with RDF-JSON.
This is just one example, but in our experience it is typical – I have not 
made up an atypical example to make a point – and it doesn't actually 
matter if the language is Javascript, Python or Ruby. The essential 
difference derives from the following:

1.      You often know in the code what subject you are looking for, 
either as a constant or in the value of a variable. With RDF-JSON, you 
just index the structure with that key. With JSON-LD, you have to loop 
through the subjectNodes looking for the one whose '@id' matches your 
known subject. You could use fancier programming constructs, like select 
or reduce, to find the subjectNode you are looking for, but it still does 
not match the simplicity of a simple hash/dictionary access in RDF-JSON.
2.      If you are looking for predicates, you have to filter out the '@id
' entries, which are artifacts of the format and don't correspond to 
triples.

We like JSON-LD and it has a use in our applications. However, it is not 
good for everything, and we are finding that a mixture of JSON-LD with 
RDF-JSON is much more useful than either one of them alone. (We are also 
using RDFa, but that is a separate story.) We do not think W3C should try 
to decide whether RDF-JSON or JSON-LD is better, and picking one as a 
winner. That would be very similar to trying to decide whether 
dictionary/hash/associative-array (RDF-JSON) is better than list/array 
(JSON-LD) and forcing a programming language to have only one of them. 
We'd like to see W3C recognize both, perhaps with some tutorial material 
that shows when to use each.

Regards.
--
Arnaud  Le Hors - Software Standards Architect - IBM Software Group

Received on Wednesday, 10 April 2013 18:02:59 UTC