JSON-LD blank node to nested object in Apache Jena

送分小仙女□ 提交于 2019-12-19 21:54:35

问题


I have the following example Turtle document:

@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" .

I converted it into JSON-LD with the Apache Jena (RDFDataMgr with JSONLD_COMPACT_PRETTY) to 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"
    }
  ]
}

But actually I want to have a nested object instead of a blank node:

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

Is that possible with Apache Jena? And is it semantically equivalent?


回答1:


Apache Jena uses jsonld-java for JSON-LD input and output.

It is possible to setup the jsonld-java output as shown with:

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/ExJsonLD.java

You'll need to consult jsonld-java as to whether the writer can do what you want.




回答2:


You can use jsonld-java with framing to convert your JSON-LD result to nice nested JSON. The result of the conversion will be semantically equivalent.

Try

    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);
        }
    }

This will produce

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


来源:https://stackoverflow.com/questions/50588066/json-ld-blank-node-to-nested-object-in-apache-jena

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!