【问题标题】:JSON-LD to normal JSONJSON-LD 到普通 JSON
【发布时间】:2017-02-22 17:12:16
【问题描述】:

我正在尝试将 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 json-ld


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2015-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-09
      相关资源
      最近更新 更多