【问题标题】:Remove extra parameters from framed JSON-LD从框架的 JSON-LD 中删除额外的参数
【发布时间】:2020-10-01 13:16:05
【问题描述】:

那么,让我们考虑以下从 db 获取的数据:

[
  {
    "@id": "http://example.com/1",
    "http://example.com/label": "Parent",
    "http://example.com/status": "Active",
    "http://example.com/children": [
      {
        "@id": "http://example.com/2"
      }
    ]
  },
  {
    "@id": "http://example.com/2",
    "http://example.com/label": "Child",
    "http://example.com/status": "Active"
  }
]

还有框架:

{
  "@context": {
    "@base": "http://example.com/",
    "@vocab": "http://example.com/"
  },
  "@graph": {
    "status":{}
  }
}

结果将如下所示:

{
  "@context": {
    "@base": "http://example.com/",
    "@vocab": "http://example.com/"
  },
  "@graph": [
    {
      "@id": "1",
      "children": {
        "@id": "2",
        "label": "Child",
        "status": "Active"
      },
      "label": "Parent",
      "status": "Active"
    },
    {
      "@id": "2",
      "label": "Child",
      "status": "Active"
    }
  ]
}

正如您在第一个对象中看到的,在 children 部分中,除了 id 之外,我还获得了一些额外的参数。

有没有办法可以简化 children 列表以仅包含 ID:

"children": [
    "2"
]

我尝试将它添加到我的框架中:

"children": {
  "@id": "http://example.com/children",
  "@type": "@id"
}

但它并没有像我预期的那样工作。

【问题讨论】:

    标签: json rdf semantic-web json-ld


    【解决方案1】:

    使用framing flags"@embed": "@never""@explicit": true

    {
      "@context": {
        "@base": "http://example.com/",
        "@vocab": "http://example.com/"
      },
      "@graph": {
        "status": {},
        "@embed": "@never"
      }
    }
    

    {
      "@context": {
        "@base": "http://example.com/",
        "@vocab": "http://example.com/"
      },
      "@graph": {
        "status": {},
        "children": {"@explicit": true, "@omitDefault": true}
      }
    }
    

    但也许你只需要compaction

    如果您不想压缩数组,请切换相应的option。在 JSONLD-Java 中:

    final JsonLdOptions options = new JsonLdOptions();
    options.setCompactArrays(false);
    

    游乐场:123

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多