Skip to content

Instantly share code, notes, and snippets.

@EmmanuelOga
Created November 27, 2020 09:34
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 EmmanuelOga/a6555bc475a8291e70c96105e60c87b7 to your computer and use it in GitHub Desktop.
Save EmmanuelOga/a6555bc475a8291e70c96105e60c87b7 to your computer and use it in GitHub Desktop.
From JSON-LD to TriG, requires "jsonld" and "n3" npm packages.
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);
});
@EmmanuelOga
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment