从框架JSON-LD中删除额外参数

pkmbmrz7  于 2023-10-21  发布在  其他
关注(0)|答案(2)|浏览(94)

因此,让我们考虑从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"
}

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

xjreopfe

xjreopfe1#

使用框架标志:"@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}
  }
}

但也许你所需要的只是压缩。
如果你不想压缩数组,切换相应的选项。在JSONLD-Java中:

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

Playground:1,2,3。

9cbw7uwe

9cbw7uwe2#

利用该帧

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

这就是结果

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

相关问题