Warm tip: This article is reproduced from serverfault.com, please click

A context for a top level index-only key in JSON-LD

发布于 2020-12-04 17:31:50

I am trying to use JSON-LD to make a legacy JSON file machine readable. The JSON file in question has a top-level object with an item member referencing an object that contains the actual relevant data.

{
  "item": {
    "id": "123",
    "name": "Test"
  }
}

When I now introduce a context like the following:

{
  "@context": {
    "name": "https://example.org/name"
  }
}

No knowledge is extracted meaning no triples can be interpreted from this. To make it work, I realized I could do the following:

{
  "@context": {
    "item": "https://example.org/exists",
    "name": "https://example.org/name"
  }
}

This one works, meaning a triple with the name predicate is extracted nicely. The problem with this approach however is, that an unnecessary triple with the exists predicate is also extracted.

Therefore I experimented with marking the top-level object as an index container, so that the item key is completely ignored. I just don't find a way to do this without the need to assign an IRI to the item term and if I assign one, again the unnecessary triple is extracted again.

{
  "@context": {
    "name": "https://example.org/name"
  }
}

No knowledge is extracted meaning no triples can be interpreted from this. To make it work, I realized I could do the following:

This doesn't work at all (because the item term has no IRI):

{
  "@context": {
    "item": {
      "@container": "@index"
    },
    "name": "https://example.org/name"
  }
}

And this does work but has the unnecessary triples again:

{
  "@context": {
    "@container": "@index",
    "item": "https://example.org/exists",
    "name": "https://example.org/name"
  }
}

What am I missing here? How can I create a context that only extracts the name triple?

Questioner
aef
Viewed
1
Stanislav Kralin 2020-12-08 09:10:36

Check this against your JSON-LD processor:

{
  "item": {
    "id":   "123",
    "name": "Test"
  } ,
  "@context": {
    "@base": "http://example.com/id/",
    "id":    "@id", 
    "item":  "@nest",
    "name":  "https://example.org/name"
  } 
}

In JSON-LD Playground, this context produces the following single RDF triple:

<http://example.com/id/123> <https://example.org/name> "Test" .

JSON-LD features used:

You could also be interested in RML.