Question

I have a Sparql Endpoint(openrdf sesame) on my local machine. I am able to get the results of the queries(6) in json (using sparql-client node module). Now, I need to convert this json into json-ld so that I can easily display it in the jade file. How can I do this without using rdfstore-js in my node.js application? I did read about jsonld node module. The json data is quite huge and so I cant specify the context for it.

Problem with rdfstore-js: Since the endpoint is on my local machine, I am facing the cross domain issue. So, I tried to load data from remote sparql endpoints into the store but it does not work. https://github.com/antoniogarrote/rdfstore-js/issues/20

Alternatively, I am open to suggestions wherein I can display data without json-ld.

Edit: After reading Gregg' suggestion, I tried to use CONSTRUCT query. However, the sesame server returns response of the query as text/plain. I tried the content negotiation by giving accept header for json-ld. But it would fail - gives me 'No acceptable file format found'.

Thanks!


I am now using http nodejs module as below. I get the response as text/plain. Is there a way to convert plain text into json-ld ?

var queryString = syllPrefix+" CONSTRUCT { ?uri ?p ?o } where  { ?uri a syll:Person . ?uri ?p ?o}";
var encodedquerystring = encodeURIComponent(queryString);
var options = {
        host: 'localhost',
        port: 8080,
        path: '/openrdf-sesame/repositories/myrepo?query=' +encodedquerystring,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Accept': 'text/plain',
        },
      };


var req = http.get(options, function(res) {
  //  console.log("Got response: " + res.statusCode);
   // console.log('HEADERS: ' + JSON.stringify(res.headers));
    var data = "";
    res.on('data', function (chunk) {
      data += chunk;
    });
    res.on('end', function () {
      console.log (data);
    });
  }).on('error', function(e) {
    console.log("Got error: " + e.message);
  });
  req.end();
Was it helpful?

Solution

Sesame does not support the JSON-LD format out of the box. You will need to install a Sesame-compatible JSON-LD writer library (such as jsonld-java) into your existing Sesame installation.

To do this, get the jsonld-java and jsonld-java-sesame jar files plus their dependencies (unfortunately this is a bit of a chore as you will need to include several third party dependencies, such as Apache libraries and Jackson, as well - but you can download everything from Maven Central) and drop them into your Sesame deployment directory inside Tomcat. Typically this directory should be something like [TOMCAT_DIR]/webapps/openrdf-sesame/WEB-INF/lib. Once you've done this, you'll need to restart Tomcat for Sesame to recognize the new writer.

Once this is done, supplying the correct Accept header (media type application/ld+json) should get you JSON-LD results on your graph queries.

Alternatively, you can try using the RDF 1.1 JSON Alternate Serialization format, which is not really a standard like JSON-LD is, but is supported by Sesame. Media type application/rdf+json.

OTHER TIPS

To get JSON-LD from sparql-client (at least the Ruby version), use CONSTRUCT or DESCRIBE rather than SELECT or ASK. If you're using directly, you can then serialize the resulting graph as JSON-LD (on a platform supporting this, such as Python, Ruby or Java), or if it's a remote connection, an HTTP service should allow you to content negotiation for the results using application/ld+json.

Turning SELECT or ASK into JSON-LD is more problematic, and would require a result-set vocabulary. The application/sparql+json is not easily directly interpreted as

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top