JSON-LD转换为普通JSON [英] JSON-LD to normal JSON
问题描述
我正在尝试将JSON-LD文件插入我的CouchDB.我唯一的问题是,当我插入JSON-LD文件时,由于ID没有链接在一起,因此生成的CouchDB是没有意义的.
I'm trying to insert a JSON-LD file into my CouchDB. The only problem I have is that when I insert my JSON-LD file, the resulting CouchDB is meaningless because the IDs aren't linked together.
我的JSON-LD文件的示例如下:
An example of what my JSON-LD file looks like:
"contributor": [
{
"@id": "_:N6e57c55b35b74782ada714fdc6d66bf1"
},
{
"@id": "_:N810e115dfb3348579a7b826a7548095b"
}
另一部分:
{
"@id": "_:N6e57c55b35b74782ada714fdc6d66bf1",
"@type": "Person",
"label": "Isely, Duane, 1918-"
},
{
"@id": "_:N810e115dfb3348579a7b826a7548095b",
"@type": "Person",
"label": "Cronquist, Arthur"
}
现在,贡献者"中的ID链接到第二部分的两个字段,这两个字段描述了人员.我想知道如何链接它们(正确的方式),所以我会得到如下信息:
Now the IDs in "contributor" are linking to the two fields of the second part, which is describing person. I would like to know how to link them (the correct way), so I would get something like this:
"contributor": [
{
"@type": "Person",
"label": "Isely, Duane, 1918-"
},
{
"@type": "Person",
"label": "Cronquist, Arthur"
}
推荐答案
您可以使用JSON-LD处理器为数据库重新创建更好的JSON.规定JSON-LD文档结构的一种好方法是定义一个框架.
You could use a JSON-LD processor to (re)create nicer JSON for your DB. A good possibility to prescribe the structure of JSON-LD documents is to define a frame.
从规范中引用:
JSON-LD Framing允许开发人员通过示例查询并将特定的树布局强制为JSON-LD文档.
JSON-LD Framing allows developers to query by example and force a specific tree layout to a JSON-LD document.
示例:
假设您的文档看起来像
{
"@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框架,例如
Add a JSON-LD Frame like
{
"@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处理器中,您将得到类似的东西
Throw it to a JSON-LD processor of your choice and you will get something like
{
"@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处理器.
Unfortunately framing is not equaly well supported. So the result depends on the JSON-LD processor you're using.
您可以通过从数据中删除"@"符号来进一步详细说明.只需将以下内容添加到您的上下文中:
You can elaborate further by removing "@" signs from your data. Simply add the following to your context:
"type" : "@type",
"id" :"@id"
此外,您可以在上下文文档中添加类型的缩写
Also, you can add shortenings for types to your context document
"MainResource": "http://json-ld.org/playground/MainResource"
For full code java example with rdf4j look here: How to convert RDF to pretty nested JSON using java rdf4j .
这篇关于JSON-LD转换为普通JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!