0

I was playing with fuseki and JSON-LD and noticed that fuseki removes prefixes from the attributes in the JSON-LD context. Example of JSON-LD context after been loaded from fuseki:

{
  "@context": {
    "hasPriceSpecification": {
      "@id": "http://purl.org/goodrelations/v1#hasPriceSpecification",
      "@type": "@id"
    },
    "acceptedPaymentMethods": {
      "@id": "http://purl.org/goodrelations/v1#acceptedPaymentMethods",
      "@type": "@id"
    },
    "includes": {
      "@id": "http://purl.org/goodrelations/v1#includes",
      "@type": "@id"
    },
    "page": {
      "@id": "http://xmlns.com/foaf/0.1/page",
      "@type": "@id"
    },
    "foaf": "http://xmlns.com/foaf/0.1/",
    "xsd": "http://www.w3.org/2001/XMLSchema#",
    "pto": "http://www.productontology.org/id/",
    "gr": "http://purl.org/goodrelations/v1#"
  }
}

Is it possible to return prefixed context and JSON-LD from fuseki?

Optionally returned JSON-LD can be formatted back to prefixied form with javascript by writing new context with the prefixes eg. gr:hasPriceSpecification. Is somehow possible to create prefixed context from this one using JSON-LD javascript library?

amiika
  • 115
  • 4

1 Answers1

0

Well .. i did a simple function that does the latter:

function newPrefixedContext(oldContext) {
    var namespaces = [];
    var newContext = {};

    for(var res in oldContext) {
        if(typeof oldContext[res] === 'string') {
            var char = oldContext[res].charAt(oldContext[res].length-1);
            if(char=="#" || char=="/") {
                var o = {prefix:res, namespace:oldContext[res]};
                namespaces.push(o);
                newContext[res] = oldContext[res];
            }   
        }
    }

    // Loop context and adds prefixed property names to newContext
    for(var res in oldContext) {
        if(typeof oldContext[res] != undefined) {
            for(var n in namespaces) {
                if(oldContext[res]["@id"]) {
                    if(oldContext[res]["@id"].indexOf(namespaces[n].namespace) == 0) {
                        newContext[namespaces[n].prefix+":"+res] = oldContext[res];
                        break;
                    } 
                } else {
                    if(namespaces[n].namespace!==oldContext[res] && oldContext[res].indexOf(namespaces[n].namespace) == 0) {
                        newContext[namespaces[n].prefix+":"+res] = oldContext[res];
                        break;
                    }
                }
            } 
        }
    }

    return newContext;

}

You can use it with JSON-LD javascript library to transform the JSON-LD to prefixed format like:

jsonld.compact(jsonLD, newPrefixedContext(jsonLD["@context"]), function(err, compacted) {
   console.log(JSON.stringify(compacted, null, 2));
});
amiika
  • 115
  • 4