灵活框架:JSON-LD To 框架形式 — Numio
使用 jsonld.frame 按 frame 重塑 JSON-LD:与 frame 匹配的节点(例如按 @type 匹配)成为顶层结果,它们引用的节点被嵌入其中。
将扁平的 @graph 转为以应用所需类型为根的树,例如每本 Book 都嵌入其所属的 library。
适用场景
- 将扁平的 @graph 转为以应用所需类型为根的树,例如每本 Book 都嵌入其所属的 library。
- 从较大的 JSON-LD 数据集中只选出某一 @type 的节点。
使用方法
- 将源文档粘贴到 JSON-LD 编辑器。
- 在 Frame 编辑器中编写 frame:一个 @context 加上 "@type": "Book" 这样的匹配规则。
- 查看结果;匹配到多个节点时,它们会列在 @graph 中。
JSON-LD + frame
// JSON-LD editor
{
"@context": {"@vocab": "http://schema.org/"},
"@graph": [
{"@id": "http://example.org/lib", "name": "City Library"},
{"@id": "http://example.org/b1", "@type": "Book", "name": "Dune",
"holdingArchive": {"@id": "http://example.org/lib"}},
{"@id": "http://example.org/b2", "@type": "Book", "name": "Emma",
"holdingArchive": {"@id": "http://example.org/lib"}}
]
}
// Frame editor
{"@context": {"@vocab": "http://schema.org/"}, "@type": "Book"}framing 后的 JSON-LD
{
"@context": {
"@vocab": "http://schema.org/"
},
"@graph": [
{
"@id": "http://example.org/b1",
"@type": "Book",
"holdingArchive": {
"@id": "http://example.org/lib",
"name": "City Library"
},
"name": "Dune"
},
{
"@id": "http://example.org/b2",
"@type": "Book",
"holdingArchive": {
"@id": "http://example.org/lib",
"name": "City Library"
},
"name": "Emma"
}
]
}功能与限制
这里节点引用会被替换为被引用的节点本身:library 嵌入到每本 book 中。
与 frame 不匹配的节点(library)只会作为嵌入对象出现,不会作为单独的结果。
使用 jsonld.js 的默认 framing 选项在浏览器中运行;@embed、@explicit 等标志可以在 frame 内部设置。
FAQ
如何只返回特定属性?
在 frame 中加入 "@explicit": true,并列出需要的属性,每个属性的值写为 {}。
为什么结果中有 @graph 键?
有多个节点与 frame 匹配时,结果在 @graph 中返回。只有一个匹配时,返回顶层对象。
为什么 Frame 编辑器预填了 schema.org 的 context?
这是页面的默认示例。该 context 是浏览器需要下载的远程 URL;离线使用时请替换为内联 context。下载失败时,页面会显示包含该 URL 的错误。