2017-02-22 28 views
0

我試圖插入一個JSON-LD文件到我的CouchDB中。我唯一的問題是,當我插入我的JSON-LD文件時,生成的CouchDB是沒有意義的,因爲ID沒有鏈接在一起。JSON-LD到正常的JSON

什麼我的JSON-LD文件看起來像一個例子:

"contributor": [ 
{ 
    "@id": "_:N6e57c55b35b74782ada714fdc6d66bf1" 
}, 
{ 
    "@id": "_:N810e115dfb3348579a7b826a7548095b" 
} 

,而另一部分:

{ 
    "@id": "_:N6e57c55b35b74782ada714fdc6d66bf1", 
    "@type": "Person", 
    "label": "Isely, Duane, 1918-" 
}, 

{ 
    "@id": "_:N810e115dfb3348579a7b826a7548095b", 
    "@type": "Person", 
    "label": "Cronquist, Arthur" 
} 

現在,在「貢獻者」的ID被鏈接到第二的兩個字段部分,這是描述人。我想知道如何把它們(正確的方法)鏈接,所以我會得到這樣的事情:

"contributor": [ 
{ 
     "@type": "Person", 
     "label": "Isely, Duane, 1918-" 
}, 
{ 
     "@type": "Person", 
     "label": "Cronquist, Arthur" 
} 

回答

1

你可以使用JSON-LD處理器(重新)創建你的數據庫更好的JSON。規定JSON-LD文檔結構的一個很好的可能性是to define a frame。從規格

報價:

JSON-LD幀允許開發人員通過實例來查詢並強制特定的樹佈局的JSON-LD文件。

例子:

假設你的文件看起來像

{ 
    "@context": { 
    "contributor": { 
     "@type": "@id", 
     "@id": "http://purl.org/dc/terms/contributor", 
     "@container": "@list" 
    }, 
    "label": { 
     "@id": "http://www.w3.org/2004/02/skos/core#prefLabel" 
    } 
    }, 
    "@graph": [ 
    { 
     "@type": "MainResource", 
     "@id": "_:foo", 
     "contributor": [ 
     { 
      "@id": "_:N6e57c55b35b74782ada714fdc6d66bf1" 
     }, 
     { 
      "@id": "_:N810e115dfb3348579a7b826a7548095b" 
     } 
     ] 
    }, 
    { 
     "@id": "_:N6e57c55b35b74782ada714fdc6d66bf1", 
     "@type": "Person", 
    "label": "Isely, Duane, 1918-" 
    }, 
    { 
     "@id": "_:N810e115dfb3348579a7b826a7548095b", 
     "@type": "Person", 
     "label": "Cronquist, Arthur" 
    } 
    ] 
} 

{ 
    "@context": { 
    "contributor": { 
     "@type": "@id", 
     "@id": "http://purl.org/dc/terms/contributor", 
     "@container": "@list" 
    }, 
    "label": { 
     "@id": "http://www.w3.org/2004/02/skos/core#prefLabel" 
    } 
    }, 
    "@type": "MainResource", 
    "@embed": "always" 
} 

把它添加JSON-LD框到你選擇的JSON-LD處理器和你會得到像

{ 
    "@context": { 
    "contributor": { 
     "@type": "@id", 
     "@id": "http://purl.org/dc/terms/contributor", 
     "@container": "@list" 
    }, 
    "label": { 
     "@id": "http://www.w3.org/2004/02/skos/core#prefLabel" 
    } 
    }, 
    "@graph": [ 
    { 
     "@id": "_:b0", 
    "@type": "http://json-ld.org/playground/MainResource", 
     "contributor": [ 
     { 
      "@id": "_:b1", 
      "@type": "http://json-ld.org/playground/Person", 
      "label": "Isely, Duane, 1918-" 
     }, 
     { 
      "@id": "_:b2", 
      "@type": "http://json-ld.org/playground/Person", 
      "label": "Cronquist, Arthur" 
     } 
     ] 
    } 
    ] 
} 

這裏是json-ld.org/playground

完整的例子不幸的是取景不equaly很好的支持。所以結果取決於您使用的JSON-LD處理器。

您可以通過從數據中刪除「@」符號來進一步闡述。只需添加以下到您的上下文:

"type" : "@type", 
"id" :"@id" 

此外,還可以對類型添加酥油對上下文的文檔

"MainResource": "http://json-ld.org/playground/MainResource" 

見例如json-ld.org/playground

對於rdf4j看看完整的代碼Java示例這裏:How to convert RDF to pretty nested JSON using java rdf4j