Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON-LD+Hydra link discovery

I've been thinking about how to employ JSON-LD to drive an application according to the HATEOAS principle.

For example I could have a simple entrypoint object, which defines a link:

{
  "@context": {
    "users": { "@id": "http://example.com/onto#users", "@type": "@id" }
  },
  "@id": "http://example.com/api",
  "users": "http://example.com/users"
}

And the #users predicate would be defined as a Link using Hydra:

{
  "@context": "http://www.w3.org/ns/hydra/context.jsonld",
  "@id": "http://example.com/onto#users",
  "@type": "Link"
}

All good so far: the application fetches the resource, then the onto#users resource would be dereferenced to discover the semantics.

The question is how should the implementer discover the URI of the users property from the JSON-LD document. Of course it is clearly defined in the @context in my example, but that URI could be declared as a QName:

"@context": {
  "onto": "http://example.com/onto#",
  "users": { "@id": "onto:users", "@type": "@id" }
}

or an external context could be used or multiple/nested contexts.

Is there a functionality in the Javacript JSON-LD library, which would return absolute URIs of any given property? Or is there a simple way to find it? A way which would work regardless of how the @context is structured? Something like

var jsonLd = /* some odc */
var usersUri = jsonLd.uriOf('users');
expect(usersUri).toBe('http://example.com/onto#users');

In other words I think I'm looking for a uniform API for reading the @context.

like image 334
Tomasz Pluskiewicz Avatar asked Jun 02 '14 14:06

Tomasz Pluskiewicz


1 Answers

Here's how you can do what you're asking with the JavaScript JSON-LD (jsonld.js) library:

var jsonld = require('jsonld');

var data = {
  "@context": {
    "onto": "http://example.com/onto#",
    "users": {"@id": "onto:users", "@type": "@id"}
  },
  "users": "http://example.com/users"
};

jsonld.processContext(null, [null, data['@context']], function(err, ctx) {
  if(err) {
    console.log('error', err);
    return;
  }
  var value = jsonld.getContextValue(ctx, 'users', '@id');
  console.log('users', value);
});

However, it's questionable whether this is a good idea. It sounds like maybe you just want to use jsonld.expand(), which will turn all properties into full URLs. Or, you can use jsonld.compact() to transform any JSON-LD input using a context that is well known by your application.

like image 176
dlongley Avatar answered Sep 29 '22 09:09

dlongley