3

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.

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
Brainlock
  • 243
  • 4
  • 11

1 Answers1

3

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.

Markus Lanthaler
  • 3,683
  • 17
  • 20
  • Thank you for clarifying. Makes sense. The only solution I can think of is to put the source data in a JSON object and change the content by adding the @id identifier as I won't be able to alter the JSON source file on the web server. – Brainlock Jul 28 '15 at 22:06