Brainlock Brainlock - 1 year ago 53
Python Question

JSON-LD @id with IRI results in blank node

I have a small JSON file hosted on a local web server with the following content:

json_source = {"key1": "azerty", "key2": "qwerty", "key3": "lorem", "key4": "ipsum"}


Using the RDFLib library, I'm parsing the JSON, adding some semantics using the context and serialize to N-Triples:

from rdflib import Graph

context ={"@id": "http://example.org/test",
"@context": {"dct": "http://purl.org/dc/terms/",
"foaf": "http://xmlns.com/foaf/0.1/",
"key1": {"@id": "dct:language"},
"key2": {"@id": "dct:title"},
"key3": {"@id": "dct:title"},
"key4": {"@id": "foaf:name"}
}
}

g = Graph()
rdf = g.parse('http://localhost/test.json', format='json-ld', context=context)

print rdf.serialize(format="nt")


The output results in blank nodes:

_:N6dc3aa6a68e34c36beade27af204cb6c <http://purl.org/dc/terms/language> "azerty" .
_:N6dc3aa6a68e34c36beade27af204cb6c <http://purl.org/dc/terms/title> "qwerty" .
_:N6dc3aa6a68e34c36beade27af204cb6c <http://xmlns.com/foaf/0.1/name> "ipsum" .
_:N6dc3aa6a68e34c36beade27af204cb6c <http://purl.org/dc/terms/title> "lorem" .


Somehow the @id did not resolve to http://example.org/test

However, when adding the JSON-LD to JSON-LD Playground as:

{
"@id": "http://example.org/test",
"@context": {
"dct": "http://purl.org/dc/terms/",
"foaf": "http://xmlns.com/foaf/0.1/",
"key1": {"@id": "dct:language"},
"key2": {"@id": "dct:title"},
"key3": {"@id": "dct:title"},
"key4": {"@id": "foaf:name"}
},
"key1": "azerty",
"key2": "qwerty",
"key3": "lorem",
"key4": "ipsum"


}

... it resolves to:

<http://example.org/test> <http://purl.org/dc/terms/language> "azerty" .
<http://example.org/test> <http://purl.org/dc/terms/title> "lorem" .
<http://example.org/test> <http://purl.org/dc/terms/title> "qwerty" .
<http://example.org/test> <http://xmlns.com/foaf/0.1/name> "ipsum" .


Does someone has some advice how to interpret the difference?
Thanks.

Answer Source

The problem is that the context you pass to rdflib not only contains the context (@context) but also @id. The method, however, ignores everything but the context - which is correct btw. The reason why this works in the JSON-LD playground is that you add @id property to the body of the document, not the context. It becomes clear when the document you pass to the playground is printed like this:

{
  "@context": {
    "dct": "http://purl.org/dc/terms/",
    "foaf": "http://xmlns.com/foaf/0.1/",
    "key1": { "@id": "dct:language" },
    "key2": { "@id": "dct:title" },
    "key3": { "@id": "dct:title" },
    "key4": { "@id": "foaf:name" }
  },
  "@id": "http://example.org/test",  <------------- part of the body, not the context
  "key1": "azerty",
  "key2": "qwerty",
  "key3": "lorem",
  "key4": "ipsum"
}

If you would add @id to test.json it would work for RDFlib as well.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download