Пустой узел JSON-LD для вложенного объекта в Apache Jena

У меня есть следующий пример документа 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" .

Я преобразовал его в JSON-LD с помощью Apache Jena (RDFDataMgr с JSONLD_COMPACT_PRETTY) в 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? И это семантически эквивалентно?


person linsenfips    schedule 29.05.2018    source источник


Ответы (3)


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, чтобы узнать, может ли автор делать то, что вы хотеть.

person AndyS    schedule 29.05.2018

Вы можете использовать jsonld-java с кадрированием чтобы преобразовать результат JSON-LD в красивый вложенный 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"
    }
  } ]
}
person jschnasse    schedule 30.05.2018

Вам необходимо переформатировать график как объект верхнего уровня. Вы можете использовать:

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

or

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

or

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

(т.е. объект, содержащий любой пример: реквизиты).

person K3---rnc    schedule 11.12.2020