2

I have created a Graph() in rdflib 6.2.0 and attempting to export it as JSON-LD. My expectation was that I would end up with a @context containing all the namespace prefixes and a @graph containing all the elements. I'm just simply using:

myGraph.serialize(destination = Path(outputpath), format = "json-ld")

But, I just end up with JSON-LD resembling this kind of structure:

[
  {
    "@id": "urn:uuid:dccc2ddb-7861-47fc-934c-d2a0901e368e",
    "@type": "http://xmlns.com/foaf/0.1/Organization",
  },
  ... (more resources in a flat list)
]

The docs (https://rdflib.readthedocs.io/en/stable/apidocs/rdflib.plugins.serializers.html#rdflib.plugins.serializers.jsonld.JsonLDSerializer) do not really clarify the matter at all, and it is very difficult to find any information on how to export a graph with already existing namespace bindings into a proper JSON-LD with @graph and @context parts.

There exists a Context class in https://rdflib.readthedocs.io/en/stable/apidocs/rdflib.plugins.shared.jsonld.html?highlight=shared.jsonld.context#rdflib.plugins.shared.jsonld.context.Context but without any documentation in the sources, it is very difficult to understand how it should be used. What does the method get_graph() do? Apparently I need to pass a Context instance to the json-ld serializer?

What am I doing wrong?

O-U-O
  • 69
  • 5

1 Answers1

0

When it comes to the context, reading the source:

If you set the auto_compact argument to True, the JSON-LD serializer will automatically create a context with all namespace bindings from the graph:

myGraph.serialize(destination = Path(outputpath), format="json-ld", auto_compact=True)

Or alternatively, you can pass the JSON-LD context contents as a dict, and the JSON-LD serializer will internally construct the Context object out of it:

context = {"@vocab": "http://xmlns.com/foaf/0.1/"}
myGraph.serialize(destination = Path(outputpath), format="json-ld", context=context)
Jukka Matilainen
  • 9,608
  • 1
  • 25
  • 19