Search code examples
json-ldrdflib

rdflib invalid remote context when testing on local sever


I am trying to test different contexts applied to json file. I have created a very simple context file (named mycontext.json) on my local machine (a 2019 MacPro) with the contents:

{ "name": "http://example.com#name", "age": "http://example.com#age"}

Next I start a local web server on my machine using jekyll serve. I can test that my file is accessible in the web browser by navigating to http://127.0.0.1:4000/mycontext.json or using the python requests library: requests.get("http://127.0.0.1:4000/mycontext.json").text.
Each test works fine in that it returns the context of mycontext.json.

The json file (named test-data.json) references the json-ld context. Its contents are:

{
        "@context": "http://127.0.0.1:4000/mycontext.json",
        "name:": "Bill",
        "age": "48"
}

However, when I parse this in rdflb, I get an INVALID_REMOTE_CONTEXT error.
That is, g.parse("test-data.json", format="json-ld") produces error:

:
:
~/opt/anaconda3/lib/python3.7/site-packages/rdflib_jsonld/context.py in _prep_sources(self, base, inputs, sources, referenced_contexts, in_source_url)
    205                 source = source_to_json(source_url)
    206                 if CONTEXT not in source:
--> 207                     raise errors.INVALID_REMOTE_CONTEXT
    208             else:
    209                 source_url = in_source_url

JSONLDException: invalid remote context

There are a lot of error messages about this, but this one seems most relevant.

I've tried initializing the graph using both Graph() and ConjunctiveGraph(). Same result for each.

I've also tried: * placing the context file in its own directory, and calling with: "@context": "http://127.0.0.1:4000/context/ * giving the context a non-json extension, and calling with: "@context": "http://127.0.0.1:4000/context/c1"

I'm really quite stumped at this point :(

I realized that I can programmatically parse/combine the context and data files into a single file. But, our goal is to create context files locally and test them on json files. If we are satisfied, we can then deploy the context files onto public web serve w/o also deploying the data.

I have also posted an issues on the rdflib-jsonld gihub: https://github.com/RDFLib/rdflib-jsonld/issues/80


Solution

  • I found an answer to my question. The document referenced by the "@context" key has to also contain an "@context" key.

    For example, if your json-ld document looks like this:

    { 
      "@context": "http://127.0.0.1:4000/mycontext", 
      "name:": "Bill", 
      "age": "48" 
    }
    

    The contents of http://127.0.0.1:4000/mycontext have to look like this:

    { 
      "@context": <----- This is needed. It was missing in my original post.
      { 
         "name": "http://example.com#name", "age": "http://example.com#age" 
      } 
    }