JSON-LD 空白节点到 Apache Jena 中的嵌套对象

lin*_*ips 5 java jena json-ld blank-nodes

我有以下示例 Turtle 文档:

@prefix dct:   <http://purl.org/dc/terms/> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix example: <http://example.com/vocabulary/> .
@prefix dcat:  <http://www.w3.org/ns/dcat#> .

<http://example.com/datasets/1>
        a                     dcat:Distribution ;
        example:props         [ example:prop1  "hello" ;
                                example:prop2  "1" 
                              ] ;
        dct:description       "test data" .
Run Code Online (Sandbox Code Playgroud)

我使用 Apache Jena(带有 JSONLD_COMPACT_PRETTY 的 RDFDataMgr)将它转换为 JSON-LD 到 JSON-LD:

{
  "@context": {
    "dct": "http://purl.org/dc/terms/",
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "dcat": "http://www.w3.org/ns/dcat#",
    "example": "http://example.com/vocabulary/"
  },
  "@graph": [
    {
      "@id": "_:b0",
      "example:prop1": "hello",
      "example:prop2": "1"
    },
    {
      "@id": "http://example.com/datasets/1",
      "@type": "dcat:Distribution",
      "example:props": {
        "@id": "_:b0"
      },
      "dct:description": "test data"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

但实际上我想要一个嵌套对象而不是一个空白节点:

{
  "@context": {
    "dct": "http://purl.org/dc/terms/",
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "dcat": "http://www.w3.org/ns/dcat#",
    "example": "http://example.com/vocabulary/"
  },
  "@graph": [
    {
      "@id": "http://example.com/datasets/1",
      "@type": "dcat:Distribution",
      "example:props": {
         "example:prop1": "hello",
         "example:prop2": "1"
      },
      "dct:description": "test data"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

Apache Jena 可以实现吗?它在语义上是等价的吗?

And*_*dyS 2

Apache Jena 使用 jsonld-java 进行 JSON-LD 输入和输出。

可以设置jsonld-java输出,如下所示:

https://jena.apache.org/documentation/io/rdf-output.html#json-ld ==> https://github.com/apache/jena/blob/master/jena-arq/src-examples/ arq/examples/riot/Ex_WriteJsonLD.java

您需要咨询jsonld-java以了解编写器是否可以执行您想要的操作。