Created
November 15, 2019 16:23
-
-
Save dlongley/92b76fb43039893091a5a3b6a3b6aa9e to your computer and use it in GitHub Desktop.
Convert native types
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
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