JSON 到 JSON LD,对原始 JSON 的更改很小(所有更改都在 JSON-LD 上下文中)

JSON to JSON LD with minimal changes to the original JSON (all changes in JSON-LD context)

TL;DR

如果我有一个像

这样的 JSON 文件
{
  "policyid": "http://example.com/policy:0099",
  "policytype": "http://www.w3.org/ns/odrl/2/Set" 
}

我想要一个类似于

的JSON-LD文档
{
  "@context": {
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "policytype": { "@id": "rdf:type",  "@type": "@id" }
   }
   "@id" : "http://example.com/policy:0099",
   "policytype": "http://www.w3.org/ns/odrl/2/Set"
}

是否可以不更改 name/vale 对 { "policyid": "http://example.com/policy:0099" } to { "@id" : "http://example.com/policy:0099" } 而是在上下文中说点什么 "policyid" -> " @ID”。

{
  "@context": {
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "policytype": { "@id": "rdf:type",  "@type": "@id" },
    #### something here that says "policyid" -> "@id"
   }
   "policyid" : "http://example.com/policy:0099",
   "policytype": "http://www.w3.org/ns/odrl/2/Set"
}

我正在查看规范示例,但找不到如何执行此操作。

更多上下文

假设我们有一个模型,它具有 RDF 规范和 JSON 编码,例如 ODRL 2.1 Ontology and ODRL Version 2.1 JSON Encoding

我想从 JSON 开始,通过映射 JSON 编码到 ODRL ontology.

生成 JSON-LD
{
  "policyid": "http://example.com/policy:0099",
  "policytype": "http://www.w3.org/ns/odrl/2/Set",  
  "permissions": [{
     "target": "http://example.com/asset:9898",
     "action": "http://www.w3.org/ns/odrl/2/reproduce"
  }]
}

以下是我要将此 json 转换为的 RDF 模型。 (我会把 Turtle 序列化以使其更具可读性)。

@prefix odrl: <http://www.w3.org/ns/odrl/2/> .

<http://example.com/policy:0099> a odrl:Set .
<http://example.com/policy:0099> odrl:permission _:perm0 .
_:perm0 odrl:action <http://www.w3.org/ns/odrl/2/reproduce> .
_:perm0 odrl:target <http://example.com/asset:9898> . 

我几乎可以通过以下上下文进行最少的更改来做到这一点。

{
  "@context": { 
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "odrl": "http://www.w3.org/ns/odrl/2/",
    "policytype": { "@id": "rdf:type",  "@type": "@id" },
    "permissions": { "@id": "odrl:permission",  "@type": "@id"},
     "target" : {"@id": "odrl:target",  "@type": "@id" },
     "action" : {"@id": "odrl:action",  "@type": "@id" }
  },
  "@id": "http://example.com/policy:0099",
  "policytype": "http://www.w3.org/ns/odrl/2/Set",
  "permissions": [{ 
       "target": "http://example.com/asset:9898",
       "action": "http://www.w3.org/ns/odrl/2/reproduce" }]
}

但是如果我想保持原来的 JSON 不变,有没有办法在上下文中说 "policyid" -> "@id"?

非常感谢!

Is it possible to not change the name/vale pair { "policyid": "http://example.com/policy:0099" } to { "@id" : "http://example.com/policy:0099" } but rather say something in the context to say "policyid" -> "@id".

是的,您只需将 policyid 映射到 @id。这叫做keyword aliasing in the spec。所以你的例子看起来像这样:

{
  "@context": {
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "policyid": "@id",
    "policytype": { "@id": "rdf:type",  "@type": "@id" }
   },
   "policyid" : "http://example.com/policy:0099",
   "policytype": "http://www.w3.org/ns/odrl/2/Set"
}