Created
November 27, 2020 09:34
-
-
Save EmmanuelOga/a6555bc475a8291e70c96105e60c87b7 to your computer and use it in GitHub Desktop.
From JSON-LD to TriG, requires "jsonld" and "n3" npm packages.
This file contains 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
import * as jsonld from "jsonld"; | |
import * as n3 from "n3"; | |
const doc = { "YOUR" : "JSONLD" }; | |
const nquads = await jsonld.toRDF(doc, { format: "application/n-quads" }); | |
const parser = new n3.Parser({ format: "application/n-quads" }); | |
const writer = new n3.Writer({ format: "application/trig" }); | |
await parser.parse(nquads, (error, quad, prefixes) => { | |
if (error) console.log(`PARSE ERROR: ${error}`); | |
if (quad) writer.addQuad(quad); | |
}); | |
await writer.end((error, result) => { | |
if (error) console.log(`WRITE ERROR: ${error}`); | |
console.log(result); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There must be a way to not write nquads first but since jsonld and n3 use different underlying representation of quads, going through plaintext is the quick and dirty way.