Skip to content

Instantly share code, notes, and snippets.

@dlongley
Created November 15, 2019 16:23
Show Gist options
  • Save dlongley/92b76fb43039893091a5a3b6a3b6aa9e to your computer and use it in GitHub Desktop.
Save dlongley/92b76fb43039893091a5a3b6a3b6aa9e to your computer and use it in GitHub Desktop.
Convert native types
const jsonld = require('jsonld');
const input = {
"@context": {
"@vocab": "http://vocab.example/"
},
"age": {
"@value": "27",
"@type": "http://www.w3.org/2001/XMLSchema#integer"
}
};
(async () => {
const rdf = await jsonld.toRDF(input);
const json = await jsonld.fromRDF(rdf, {useNativeTypes: true});
const compacted = await jsonld.compact(json, input['@context']);
console.log(JSON.stringify(compacted, null, 2));
})();
const frame = {
"@context": input["@context"]
};
(async () => {
const rdf = await jsonld.toRDF(input);
const json = await jsonld.fromRDF(rdf, {useNativeTypes: true});
const framed = await jsonld.frame(json, frame);
// once jsonld.js is updated to 1.1 this step can be skipped
const output = {
"@context": framed["@context"],
...framed["@graph"][0]
};
console.log(JSON.stringify(output, null, 2));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment