Ad

In JSON-LD, Is It Possible To Extend A Context?

- 1 answer

I have a JSON-LD document.

{
  "@id": "VDWW1LL3MZ",
  "first_name": "Vincent",
  "last_name": "Willems",
  "knows":["MartyP"],
  "@context": {
    "foaf": "http://xmlns.com/foaf/0.1/",
    "first_name": "foaf:givenName",
    "last_name": "foaf:familyName",
    "knows": "foaf:knows",
    "MartyP": { 
      "@id": "http://example.com/martyp",
      "first_name": "Marty",
      "last_name": "P"
    }
  }
}

Now, part of the context of this document is generated in run-time (the Marty P object), but the foaf prefix definition is static, and repeated for each document.

If I have like 10 prefix definitions, it feel wasteful to put them in each and every document. So I would like to do something like

generated document:

{
  "@id": "VDWW1LL3MZ",
  "first_name": "Vincent",
  "last_name": "Willems",
  "knows":["MartyP"],
  "@context": {
    "@extends": "http://example.com/base_context.jsonld",
    "MartyP": { 
      "@id": "http://example.com/martyp",
      "first_name": "Marty",
      "last_name": "P"
    }
  }
}

base_context.jsonld:

  {
    "foaf": "http://xmlns.com/foaf/0.1/",
    "first_name": "foaf:givenName",
    "last_name": "foaf:familyName",
    "knows": "foaf:knows"
  }

Is this possible?

Ad

Answer

Each @context can actually be multiple objects (or URLs), which are then combined in the order that they appear (so that it is possible to change meaning of terms - caution there).

To do that you use an array, where you can mix local and external contexts. Here's your example

{
  "@context": 
  [
    "http://example.com/base_context.jsonld",
    {
      "@vocab": "http://example.com/"
    }
  ]
}

It's described in section 6.7 of JSON-LD specs.

Ad
source: stackoverflow.com
Ad