linkeddata / rdflib.js

Linked Data API for JavaScript
http://linkeddata.github.io/rdflib.js/doc/
Other
566 stars 146 forks source link

How to use rdflib with jsonld #78

Open jamsden opened 9 years ago

jamsden commented 9 years ago

I have had good success reading RDF/XML, Turtle and JSON-LD resources with rdflib, and doing in-memory SPQRQL-like queries on the graphs. However, I would like to explore using jsonld.js so that applications can use native JSON and sift.js instead of the rdflib IndexedFormulas and queries.

Unfortunately jsonld.js only parses application/nquads. But it has a means of registering other parsers. I tried to create a jsonld parser for RDF/XML using rdf-ext:

var rdf = require('rdf-ext')();
var rdfXmlParser = function(input, callback) {
    var parser = new rdf.RdfXmlParser();
    parser.parse(input, function doneParsing(dataset) {
        callback(undefined, {'graph': dataset.toArray()});
    });
}
jsonld.registerRDFParser('application/rdf+xml', rdfXmlParser);
jsonld.fromRDF(rs_xml.toString(), {}, function loadRDFXML(err, doc) {
...

But this doesn't work since the RDF dataset format expected by jsonld requires the triples to be URIs or primitive types, not the objects that are created by the rdf-ext parsers.

I noticed that rdflib.js wraps jsonld. Is there a way to use the wrapped jsonld fromRDF() method to convert an rdflib graph into JSON that can be used by jsonld and sift.js? Of is there a better way to accomplish what I'm trying to do?

Thanks for the help.

jamsden commented 9 years ago

I propose rdflib.js be extended with two new methods: toJSONLD and fromJSONLD that convert a Formula to/from JSON-LD. This will allow applications to use JavaScript and modules like sift.js to query knowledge bases in a familiar, language friendly manner.

Possibly the best way to implement this would be to change the jsonld.js RDF dataset format to be rdflib.js Formula so that these two modules could be used together.

Does this sound reasonable? Is it a good idea? I'd be willing to put some effort into this if the community thinks it would be valuable.

timbl commented 8 years ago

When you mean convert it to JSON/LD, I feel you mean to a set of nested JS objects, not to the string form?

jamsden commented 8 years ago

Yes, JSON-LD objects, not a string. JSON can take care of creating the strings, that's already supported. What I want to do is be able to read and write JSON-LD and compliment rdflib.js BGS query mechanisms with things like sift.js. Also LDP requires Turtle and JSON-LD, so that allows rdflib to support LDP.

Perhaps this could be implemented by normalizing the internal format of rdflib.js and jsonld.js.

angelo-v commented 5 years ago

Would be great to have JSON-LD support in rdflib.js. JSON(-LD) is the most convenient format to use in JavaScript apps. All web developers can use it, even those who are not aware of RDF.

josephguillaume commented 4 years ago

As noted in the original post, rdflib does already support reading json-ld with parse, and it appears serialisation works by converting to ntriples (https://github.com/linkeddata/rdflib.js/blob/master/src/serialize.ts#L72), then to quads, and finally using jsonld.fromRDF (https://github.com/linkeddata/rdflib.js/blob/master/src/convert.js#L25)

There's an example here too: https://stackoverflow.com/questions/54043762/convert-or-translate-a-rdf-xml-file-to-json-ld-format

It seems like this issue may be talking about a broader concept of querying a store in JSON format, but if not, perhaps it has now been addressed?