Skip to content

Instantly share code, notes, and snippets.

@phochste
Last active November 16, 2022 10:41
Show Gist options
  • Save phochste/ef249cccffbcb6583782526f0fe65b57 to your computer and use it in GitHub Desktop.
Save phochste/ef249cccffbcb6583782526f0fe65b57 to your computer and use it in GitHub Desktop.
Example how to transform RDF from one serialisation to another
// npm install rdf-parse rdf-serialize stream-to-string streamify-string'
// node example.js
const rdfParser = require("rdf-parse").default;
const rdfSerializer = require("rdf-serialize").default;
const stringifyStream = require('stream-to-string');
const streamifyString = require('streamify-string');
doit(`
{
"@context": "http://schema.org/",
"@type": "Person",
"name": "Jane Doe",
"jobTitle": { "@list": ["Professor", "Master" ] } ,
"telephone": "(425) 123-4567",
"url": "http://www.janedoe.com"
}
`,
'application/ld+json',
'text/turtle'
);
async function doit(data,inType,outType) {
const inStream = streamifyString(data);
const quadStream = rdfParser.parse(inStream, { contentType: inType });
const outStream = rdfSerializer.serialize(quadStream, { contentType: outType });
console.log(await stringifyStream(outStream));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment