我正在尝试将JSON-LD文件插入到我的CouchDB中。我唯一的问题是当我插入我的JSON-LD文件时,生成的CouchDB没有意义,因为ID没有链接在一起。
我的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"
}
您可以使用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"
}
]
}
像添加一个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"
}
},
"@type": "MainResource",
"@embed": "always"
}
把它扔给你选择的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中的完整示例
不幸的是,框架不能得到很好的支持。所以结果取决于你正在使用的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。