JSON到JSON LD对原始JSON的最小更改(JSON-LD上下文中的所有更改)

时间:2015-11-07 13:20:31

标签: json rdf json-ld

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"
}

是否可以不更改名称/值对{" policyid":" http://example.com/policy:0099"到{" @ id" :" http://example.com/policy:0099"而是在上下文中说出" policyid" - > " @ ID&#34 ;.

{
  "@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 OntologyODRL Version 2.1 JSON Encoding

我想从JSON开始,通过将JSON编码映射到ODRL本体来生成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,有没有办法说&#34; policyid&#34; - &GT; &#34; @ ID&#34;在上下文中?

非常感谢!

1 个答案:

答案 0 :(得分:7)

  

是否可以不更改名称/值对{&#34; policyid&#34;:   &#34; http://www.tldp.org/LDP/abs/html/io-redirection.html&#34;到{&#34; @ id&#34; :   &#34; http://example.com/policy:0099&#34;而是说出一些东西   上下文&#34; policyid&#34; - &GT; &#34; @ ID&#34;

是的,您只需将policyid映射到public static implicit operator int (Item item) { if (item == null) { return default(int); //0 } return item.number; } 即可。这称为http://example.com/policy:0099。所以你的例子看起来像这样:

@id