0

I am getting accustomed to the Ruby RDF JSON-LD toolset and currently I'm trying to extract knowledge from plain JSON files which do not carry a JSON-LD context. Therefore I need a way to set a locally-provided context when loading them.

So what I am doing is using JSON::LD::API.toRdf. But it seems I can't set a local context directly in its interface. How can it be done?

aef
  • 4,498
  • 7
  • 26
  • 44

1 Answers1

1

The JSON-LD API (Spec and Ruby) describes a expandContext option. In Ruby, this can take a string (interpreted as a URL), a something that responds to #read (such as a StringIO or File), a Hash, Array, or an instance of JSON::LD::Context.

Typically, if you want to pass a locally-provided context, you can do so as a Hash. A common pattern I use is the following:

context = JSON.parse(%({
  "@context": {...}
}))

graph = RDF::Graph.new

JSON::LD::API.toRdf(input, context) { |statement| graph << statement}

Have a look at the documentation here, or feel free to find me on gitter.

Gregg Kellogg
  • 1,831
  • 12
  • 8
  • Sadly, when I do as you described I get the following error when calling `toRdf`: `ArgumentError: wrong number of arguments (given 2, expected 1)`. I am using the most recet `json-ld` gem version 3.1.5. – aef Dec 06 '20 at 12:19
  • I managed to do it now, but I had to use `JSON::LD::API.toRdf(input, expandContext: context) do |statement|` and the context needed to be defined directly in the `context` variable instead of nested within a `"@context" => {…}` member. Am I doing something wrong because your described way didn't work? – aef Dec 06 '20 at 16:34
  • No, I think you have that right. Arguably, the code should look for `@context` and use that, rather than a de-referenced context. We're outside the bounds of the spec here, so it's something entirely defined by the Ruby library. If something's still not working for you, file an issue at https://github.com/ruby-rdf/json-ld/issues. – Gregg Kellogg Dec 06 '20 at 22:33