从 JSON 转换为 JSON-LD

Converting to JSON-LD from JSON

我正在开展一个项目,该项目要求我们使用现有的服务 json-ld。我们说json。我仍在研究 http://www.w3.org/TR/json-ld/ 文档以及如何转换 json > json-ld(并返回)。

一些问题..如果我提供 2 个这样的上下文..

"@context": [
        "http://example/test1/core.jsonld",
        "http://example2/test2/core.jsonld"
    ],

密钥如何知道要应用哪个前缀? 例如

(test1 or test2)name : ..
(test1 or test2)date : ..
(test1 or test2)address : ..

到目前为止,我对它的工作原理的理解是,任何 json 都可以转换为 json ld。我们需要为其提供上下文,以便它知道 'iri' 或名称空间。

此外,我正在使用的当前示例 json-ld 也有一些没有定义前缀的键,所以我想有一个默认值,但没有任何东西告诉我默认值是什么(没有@vocab,字面上只是定义的两个上下文)。我想默认值是一个上下文,但在这种情况下我有 2.

如果我错了请纠正我,但所有 json 都可以转换为 json-ld,反之亦然?我正在寻找 Java 解决方案 ATM。

我想要的一个非常基本的例子

{
    "id": 1,
    "name": "A green door",
    "price": 12.50,
}

成为

{
    @context{
     test : www.example.com/test/core.json-ld
     test2 : www.example.com/test2/core.json-ld
     }
    "id": 1,
    "test:name": "A green door",
    "test2:price": 12.50,
}

编辑:这里是服务对我的期望的示例

{
    "@context": [
        "http://test.net/abcd/contexts/core.jsonld",
        "http://test.net/abcd/contexts/def/4.0/core.jsonld"
    ],

    "startDate": { "@value": "2009-11-21T22:17:10Z", "@type": "xsd:dateTime" },
    "endDate": { "@value": "2005-13-24T22:01:01Z", "@type": "xsd:dateTime" },
    "comment": "my comment",
    "example": [{
      "properties": {
        "name": "test name",
        "description": "test description",
        "geometry": { "objVal": "POINT (127.25 3.243)", "confidence": "abcd-c:MED" },
        "def:width": { "decimalVal" : 50, "units": "abcd-u:meters" },
        "def:length": { "decimalVal" : 75, "units": "abcd-u:meters" },
        "def:height": { "decimalVal" : 200, "units": "abcd-u:meters" },
        "def:status": "operational",
        "def:typeCode": "building"
      },
      "metadata": {
        "@id": "object/123-32132-12321f",
        "type": [ "def:Building" ],
        "def:confidence": { "@id": "abcd-c:HIGH" },
        "def:typeCode": "building"
      }
    }],
    "def:classification": "classified",
    "def:confidence": { "@id": "abcd-c:HIGH" },
    "def:indicator": { "@value": "true", "@type": "xsd:boolean" },
    "def:levelCode": { "@id": "def-cl:FULL" },
    "source": {
      "@id": "remote-store/12321fd-cdsf143221",
      "mediaType": "image/jpeg",
      "startDate": { "@value": "2001-11-11T12:02:02Z", "@type": "xsd:dateTime" },
      "def:classification": "classified",
      "def:producer": "ABC",
      "name": "another name",
      "describedby": "source/123123123-f21321f" 
    }
}

我当前的 json 当然看起来不是这样的,我可以将它构造成类似的,但我觉得如果我所要做的只是将 @context 添加到我的当前 json 并将其传递给服务,因为它向我展示了它的期望。听起来我也许可以做到这一点,并且让现有的服务读取 json 和注释与我提供的示例 json-ld 相同

首先,根据定义,每个 JSON-LD 文档也是一个有效的 JSON 文档。

How do the key's know which prefix to apply?

上下文按顺序求值。这意味着您示例中 test2/core.jsonld 中的术语定义将覆盖 test1/core.jsonld 中的定义。这在“Advanced Context Usage”部分的 JSON-LD 规范中有更详细的描述。

So far to my understanding of how this works, is that any json can be converted to json ld. We need to supply it a context, so it knows the 'iri' or namespace.

对于 大多数 文档来说是正确的。有一些形式无法仅通过添加上下文映射到合理的 JSON-LD。

Also, the current example json-ld I am working with also has some key's with no prefix defined, so I imagine there is a default, but there is nothing that shows me what the default is(no @vocab, literally just the two context defined). I imagine the default would be a context, but in this case I have 2.

找出幕后情况的最简单方法是使用 JSON-LD playground 并展开文档。这摆脱了上下文并显示了处理器如何解释数据。展开文档时,将忽略(即丢弃)未映射到 IRI 的术语。

编辑:

my current json of course does not look like this, I can structure it similar but I feel it would be too easy if all I had to do was simply add the @context to my current json and pass it to the service, since it shows me what it expects.

这确实是最简单的解决方案。

I am still confused on how the actual conversion from json to ld happens.

JSON-LD 也就是 JSON。所以那里没有魔法.. 最简单的事情,如果可以的话,就是把它变成其他服务期望的形式。

I am hoping there is some type of library where i just supply it my json and give it a context and it does the conversion for me? is something like this available/possible? My use case is.... I create a json object from the ui, I send this json to my rest service which calls a service, in this service I want to convert it to json ld before calling another service which only understands ld.

这也是可能的,但可能更复杂。您需要将 JSON-LD 处理器的 compact method with the expandContext 选项设置为上下文,将 您的 键映射到正确的 IRI,并将上下文参数设置为上下文(s) 其他服务期望。如果其他服务确实使用 JSON-LD,则没有必要压缩它以使用相同的上下文。只需通过传递 expandContext 来扩展它就足够了。

Lets say the pre-existing service supplies me their context for example www.test.com, what else needs to be done/supplied?

我猜没什么