【问题标题】:JSON-LD blank node to nested object in Apache JenaApache Jena 中嵌套对象的 JSON-LD 空白节点
【发布时间】:2018-11-08 07:52:24
【问题描述】:

我有以下示例 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" .

我使用 Apache Jena(RDFDataMgr 和 JSONLD_COMPACT_PRETTY)将它转换为 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"
    }
  ]
}

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

{
  "@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"
    }
  ]
}

Apache Jena 有可能吗?它在语义上是否等效?

【问题讨论】:

    标签: java jena json-ld blank-nodes


    【解决方案1】:

    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作者是否可以做你想做的事。

    【讨论】:

      【解决方案2】:

      您可以使用带有框架的jsonld-java 来转换您的JSON-LD result to nice nested JSON。转换的结果在语义上是等价的。

      试试

          private static String getPrettyJsonLdString(String rdfGraphAsJson) {
              try {
              //@formatter:off
                      return JsonUtils
                              .toPrettyString(
                                      getFramedJson(
                                              createJsonObject(
                                                              rdfGraphAsJson)));
              //@formatter:on
              } catch (Exception e) {
                  throw new RuntimeException(e);
              }
          }
      
      
          private static Map<String, Object> getFramedJson(Object json) {
              try {
                  return JsonLdProcessor.frame(json, getFrame(), new JsonLdOptions());
              } catch (Exception e) {
                  throw new RuntimeException(e);
              }
          }
      
          private static Map<String, Object> getFrame() {
              Map<String, Object> frame = new HashMap<>();
              /*
                Use @type to define 'root' object to embed into
              */
              frame.put("@type" , "dcat:Distribution");
              Map<String,Object>context=new HashMap<>();
              context.put("dct", "http://purl.org/dc/terms/");
              context.put("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
              context.put("dcat", "http://www.w3.org/ns/dcat#");
              context.put("example", "http://example.com/vocabulary/");
              frame.put("@context", context);
              return frame;
          }
      
          private static Object createJsonObject(String ld) {
              try (InputStream inputStream =
                      new ByteArrayInputStream(ld.getBytes(Charsets.UTF_8))) {
                  Object jsonObject = JsonUtils.fromInputStream(inputStream);
                  return jsonObject;
              } catch (Exception e) {
                  throw new RuntimeException(e);
              }
          }
      

      这会产生

      {
        "@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" : {
            "@id" : "_:b0",
            "example:prop1" : "hello",
            "example:prop2" : "1"
          }
        } ]
      }
      

      【讨论】:

        【解决方案3】:

        您需要将re-frame 图形作为顶级对象。您可以使用:

        {
          "@context": ...,
          "@type": "dcat:Distribution"
        }
        

        {
          "@context": ...,
          "@id": "http://example.com/datasets/1"
        }
        

        {
          "@context": ...,
          "example:props": {}
        }
        

        (即包含任何“example:props”的对象)。

        【讨论】:

          猜你喜欢
          • 2017-12-31
          • 1970-01-01
          • 1970-01-01
          • 2019-12-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多