您可以使用 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。