Skip to content

Instantly share code, notes, and snippets.

@RubenVerborgh
Created October 21, 2016 07:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RubenVerborgh/8da43c6d27d4ba0ef67f8bb2af38de36 to your computer and use it in GitHub Desktop.
Save RubenVerborgh/8da43c6d27d4ba0ef67f8bb2af38de36 to your computer and use it in GitHub Desktop.
Turtle to JSON-LD
const N3 = require('n3'),
fs = require('fs');
const stream = fs.createReadStream('file.ttl');
var first = true;
console.log('{');
const triples = new N3.Parser().parse(stream, (error, triple) => {
if (triple) {
if (first)
first = false
else
console.log(',');
console.log(convertTriple(triple));
}
else {
console.log(first ? '{}' : '}');
}
});
function convertTriple({ subject, predicate, object }) {
return JSON.stringify({
"@id": subject,
[predicate]: N3.Util.isLiteral(object) ? N3.Util.getLiteralValue(object) : object,
}, null, ' ');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment