Search code examples
pythonrdfrdflibjson-ld

RDFLib parser does not recognize json-ld format


My code in Python 3.4:

from rdflib import Graph, plugin
import json, rdflib_jsonld
from rdflib.plugin import register, Serializer
register('json-ld', Serializer, 'rdflib_jsonld.serializer', 'JsonLDSerializer')

context = {
"@context": {
            "foaf" : "http://xmlns.com/foaf/0.1/",
            "vcard": "http://www.w3.org/2006/vcard/ns#country-name",
            "job": "http://example.org/job",

            "name":             {"@id": "foaf:name"},
            "country":          {"@id": "vcard:country-name"},
            "profession":       {"@id": "job:occupation"},
        }
}

x = [{"name": "bert", "country": "antartica", "profession": "bear"}]
g = Graph()
g.parse(data=json.dumps(x), format='json-ld', context=context)
g.close()

Error:

"No plugin registered for (%s, %s)" % (name, kind))
rdflib.plugin.PluginException: No plugin registered for (json-ld, <class'rdflib.parser.Parser'>)

According to the RDFLib documentation, a list of supported plugins does not include the json-ld format. However, I had it working before with format set to json-ld and there are plenty of examples using the json-ld format, e.g: https://github.com/RDFLib/rdflib-jsonld/issues/19

I included the import of rdflib_jsonld although it worked before on another environment (Python 2.7) with only the rdflib (I know, doesn't make any sense).

The register part of json-ld on line 4 isn't helping either.

Anyone having an idea?


Solution

  • I got it working by adding:

    from SPARQLWrapper import SPARQLWrapper
    

    I was looking into the jsonLayer module from RDFLib at http://rdflib.readthedocs.org/en/latest/apidocs/rdflib.plugins.sparql.results.html#module-rdflib.plugins.sparql.results.jsonlayer and noticed the mentioning of SPARQLWrapper which I used in my previous environment where I got the example working and there it was.