别名 JSON-LD 关键字并同时为其提供 @context

Alias a JSON-LD keyword and simultaneously provide a @context for it

我有以下 JSON 文档(基于 GitHub API 输出):

{
  "@id": "https://github.com/octadocs/octadocs",
  "license": {
    "key": "mit",
    "name": "MIT License",
    "spdx_id": "MIT",
    "url": "https://api.github.com/licenses/mit",
    "node_id": "MDc6TGljZW5zZTEz"
  }
}

使用 JSON-LD,我想从中检索以下三元组:

<https://github.com/octadocs/octadocs>
    <https://octadocs.io/github/license>
    <https://spdx.org/licenses/MIT> .

上下文:

{
  "@base": "https://octadocs.io/github/",
  "@vocab": "https://octadocs.io/github/",
  "spdx_id": {
    "@type": "@id",
    "@context": {
      "@base": "https://spdx.org/licenses/"
    }
  }
}

在 JSON-LD 游乐场看到一个 demonstration

但是,这会创建一个与所需结构略有不同的结构:

<https://github.com/octadocs/octadocs> <https://octadocs.io/github/license> _:b0 .
_:b0 <https://octadocs.io/github/spdx_id> <https://spdx.org/licenses/MIT> .

我想避开空白节点。

这可以通过JSON-LD关键字别名来实现:

{
  ...,
  "spdx_id": "@id"
}

但是如何同时

您要做的是删除 license 中的 @vocab 定义,将 spdx_id 别名为 @id,并删除默认词汇表。这将 license 的对象值视为节点对象(真正的节点引用),因为 spdx_id 以外的所有键都将被忽略。见游乐场 link here.

{
  "@context": {
    "@base": "https://octadocs.io/github/",
    "@vocab": "https://octadocs.io/github/",
    "license": {
      "@context": {
        "@vocab": null,
        "spdx_id": "@id"
      }
    }
  },
  "@id": "https://github.com/octadocs/octadocs",
  "license": {
    "key": "mit",
    "name": "MIT License",
    "spdx_id": "MIT",
    "url": "https://api.github.com/licenses/mit",
    "node_id": "MDc6TGljZW5zZTEz"
  }
}