JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://raw.github.com/antoniogarrote/json-ld-macros/master/src/macro.js"></script>
<script src="http://json-ld.org/playground/Future.js"></script>
<script src="http://json-ld.org/playground/jsonld.js"></script>
<h1>Response (original data)</h1>
<pre id="response"></pre>
<h1>JSON-LD</h1>
<pre id="transformed"></pre>
<h1>Expanded JSON-LD</h1>
<pre id="expanded"></pre>
JavaScript
// ------------------------------------------------------------------------------------ //
// Retrieve the representation of a GitHub user and transform it to JSON-LD //
// ------------------------------------------------------------------------------------ //
// Define a simple helper function to render JSON
var render = function(element, data) {
//alert(JSON.stringify(data, null, " "));
$(element).text(JSON.stringify(data, null, " "));
};
/// Define a macro to transform responses from JSON to JSON-LD
var macros = jsonld_macros;
macros.registerAPI({
"https://api.github.com/users/*": // we transform all responses for these URIs
{
"$": // we select the root node
{
"@context": { "@vocab":"http://example.com/gh#" }, // and a add a JSON-LD context
"@id": [ { 'f:valueof': 'url' } ] // @id is set to the value of url,
} // we could also alias url instead
}
});
var resourceURL = 'https://api.github.com/users/lanthaler';
var jqxhr = $.getJSON(resourceURL) // Retrieve the GitHub user
.then(function(res) {
render('#response', res); // Render the response
return res;
})
.then(function(data) {
var transformed = macros.resolve(resourceURL, data); // Transform to JSON-LD
render('#transformed', transformed);
return transformed;
}).then(function(transformed) {
var processor = new jsonld.JsonLdProcessor();
processor.expand(transformed).done(function(expanded) { // Expand JSON-LD
render('#expanded', expanded);
})
});