digitalbazaar / jsonld.js

A JSON-LD Processor and API implementation in JavaScript
https://json-ld.org/
Other
1.66k stars 195 forks source link

Things not strings with multiple vocabularies #444

Open pawe3kaleta opened 3 years ago

pawe3kaleta commented 3 years ago

I have JSON object containing the attributes using multiple controlled vocabularies. And I'm struggling how to describe it properly using JSON-LD context.

I'm trying to apply Best Practice #8: Things not strings in the following example:

{
  "@context": [{
    "@vocab": "http://schema.org/",
    "gender": {"@id": "gender", "@type": "@vocab"}
  }, {
    "@vocab": "http://example.org/",
     "code": {"@id": "code", "@type": "@vocab"}
  }],
  "gender": "Male",
  "code": "ABC"
}

(Open in JSON-LD Playground)

I would expect the following after expanding:

[{
    "http://example.org/code": [ { "@id": "http://example.org/ABC" } ],
    "http://schema.org/gender": [ { "@id": "http://schema.org/Male" } ]
}]

but actually I get:

[{
    "http://example.org/code": [ { "@id": "http://example.org/ABC" } ],
    "http://schema.org/gender": [ { "@id": "http://example.org/Male" } ]
}]

It seems that the property name is resolved correctly using @vocab from the context containing the property mapping. But the property value is resolved incorrectly i.e. it's resolved using @vocab from the last context regardless of the property mapping declaration.

Ideally I would like to declare @vocab at the property level (which is forbidden as for now) to instruct a JSON-LD processor, which vocabulary, should be used while expanding e.g.

{
  "@context": {
    "@vocab": "http://schema.org/",
    "gender": {"@id": "gender", "@type": "@vocab"},
    "ex": "http://example.org/",
    "code": {"@id": "ex:code", "@type": "@vocab", "@vocab": "http://example.org/"}
  },
  "gender": "Male",
  "code": "ABC"
}

(Open in JSON-LD Playground)

pawe3kaleta commented 3 years ago

Meanwhile, I figured out it's possible to achieve expected results using JSON-LD 1.1 feature about scoped contexts. But I'm still wondering if the issue can be solved using JSON-LD 1.0.

{
  "@context": {
    "@vocab": "http://schema.org/",
    "gender": {
      "@id": "gender",
      "@type": "@vocab"
    },
    "ex": "http://example.org/",
    "code": {
      "@id": "ex:code",
      "@type": "@vocab",
      "@context": {
         "@vocab": "http://example.org/" 
      }
    }
  },
  "gender": "Male",
  "code": "ABC"
}

(Open in JSON-LD Playground