Created
October 21, 2016 07:08
-
-
Save RubenVerborgh/8da43c6d27d4ba0ef67f8bb2af38de36 to your computer and use it in GitHub Desktop.
Turtle to JSON-LD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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