RDFlib serlialization in json-ld without blank node ids

31 Views Asked by At

To construct the graph with rdflib I use blank nodes, the IDs of which I do not want to serialize. I

To construct a graph:

from rdflib import URIRef, Graph, XSD, BNode, Literal, RDF

g = Graph()
bnode = BNode()

g.add((URIRef("http://example.org/data/about"), URIRef("http://purl.org/dc/terms/title"), bnode))
g.add((bnode, RDF.type, URIRef("http://example.org/onto/TextNode")))
g.add((bnode, URIRef("http://example.org/onto/hasTitle"), Literal("Title string", datatype=XSD.string)))

print(g.serialize(format='json-ld'))

The results are as follows:

[
  {
    "@id": "http://example.org/data/about",
    "http://purl.org/dc/terms/title": [
      {
        "@id": "_:N992f1250990c47d6b95d511cb6a7595d"
      }
    ]
  },
  {
    "@id": "_:N992f1250990c47d6b95d511cb6a7595d",
    "@type": [
      "http://example.org/onto/TextNode"
    ],
    "http://example.org/onto/hasTitle": [
      {
        "@value": "Title string"
      }
    ]
  }
]

I would like to have the following result:

[
    {
        "@id": "http://example.org/data/about",
        "http://purl.org/dc/terms/title": [
            {
                "@type": [
                    "http://example.org/onto/TextNode"
                ],
                "http://example.org/onto/hasTitle": [
                    {
                        "@value": "Title string"
                    }
                ]
            }
        ]
    }
]

I tried it out with and without the installation of the rdflib-jsonld plugin, and it did not make a difference.

If you serialize it as turtle or n3 then it displays the blank nodes as I would expect without the internal id.

0

There are 0 best solutions below