Is there a converter between json-ld and turtle(n3)?


#1

https://npm.runkit.com/rdf-translator uses https://bitbucket.org/alexstolz/rdf-translator, but it stops maintaining.

Is there an alternative written in JS?


#2

I think https://rdf.js.org/ is a good resource for finding stuff like this in general, but rdflib.js allows you to read JSON-LD and write to Turtle at least (not the other way around though IIRC).


#3

rdflib.js can both read and write JSON-LD. Here’s how:

function turtle2jsonld( turtleString, store, uri ){
  return new Promise(resolve=>{
    $rdf.parse( turtleString, store, uri, "text/turtle", e => {
        if(e) { console.log("Parse Error! "); return resolve(e) }
        $rdf.serialize(null,store, uri,'application/ld+json',(e,s)=>{
            if(e) { console.log("Serialize Error! "); return resolve(e) }
            return resolve(s)
        })
    })
  })
}
function jsonld2turtle( jsonldString, store, uri ){
  return new Promise(resolve=>{
    $rdf.parse( jsonldString, store, uri, "application/ld+json", e => {
        if(e) { console.log("Parse Error! "); return resolve(e) }
        $rdf.serialize(null,store, uri,'text/turtle',(e,s)=>{
            if(e) { console.log("Serialize Error! "); return resolve(e) }
            return resolve(s)
        })
    })
  })
}