jbj-jsonld
v1.0.0
Published
JBJ JSON-LD module
Downloads
10
Readme
JBJ JSON-LD module
JBJ JSON-LD is a JBJ module aiming to facilitate the generation of JSON-LD from JSON.
It can also be used in combination with JBJ-RDFa.
Contributors
Installation
$ npm install jbj-jsonld
Usage
This JBJ module cannot be used alone. JBJ has to be installed.
var JBJ = require('jbj');
JBJ.use(require('jbj-jsonld'));
Tests
Use mocha to run the tests.
$ npm install
$ npm test
Actions
Once the module is declared as used for JBJ, you can use the following actions:
<a id="context"
context: { field: {scheme, type} }
Add a context in the result.
Warning: the context is required to use the jsonld action.
Example 1: generate a valid JSON-LD @context
.
{
"input": {
},
"stylesheet": {
"context": {
"title": {
"scheme": "http://purl.org/dc/terms/title",
"type": "https://www.w3.org/TR/xmlschema-2/#string"
}
}
},
"expected": {
"@context": {
"title": {
"@id": "http://purl.org/dc/terms/title",
"@type": "https://www.w3.org/TR/xmlschema-2/#string"
}
}
}
}
Example 2: Generate a compacted JSON-LD from input
{
"input": {
"title": "An example of string value"
},
"stylesheet": {
"context": {
"title": {
"scheme": "http://purl.org/dc/terms/title",
"type": "https://www.w3.org/TR/xmlschema-2/#string"
}
},
"jsonld": "compacted"
},
"expected": {
"@context": {
"title": {
"@id": "http://purl.org/dc/terms/title",
"@type": "https://www.w3.org/TR/xmlschema-2/#string"
}
},
"title": "An example of string value"
}
}
jsonld: mode
Generate a JSON-LD from an input containing a JSON-LD context (see context), in various modes:
Example 1: Remove non-terminal properties from JSON-LD (here, the field
part)
{
"input": {
"field": "value"
},
"stylesheet": {
"$title": {
"get": "field",
"capitalize": true
},
"context": {
"title": {
"scheme": "http://purl.org/dc/terms/title",
"type": "https://www.w3.org/TR/xmlschema-2/#string"
}
},
"jsonld": "compacted"
},
"expected": {
"@context": {
"title": {
"@id": "http://purl.org/dc/terms/title",
"@type": "https://www.w3.org/TR/xmlschema-2/#string"
}
},
"title": "Value"
}
}
Example 2: expanded JSON-LD
{
"input": {
"@context": {
"access": {
"@id": "https://schema.org/isAccessibleForFree",
"@type": "https://www.w3.org/TR/xmlschema-2/#boolean"
}
},
"access": true
},
"stylesheet": {
"jsonld": "expanded"
},
"expected": [
{
"https://schema.org/isAccessibleForFree": [
{
"@type": "https://www.w3.org/TR/xmlschema-2/#boolean",
"@value": true
}
]
}
]
}
Example 3: flattened JSON-LD
{
"input": {
"@context": {
"access": {
"@id": "https://schema.org/isAccessibleForFree",
"@type": "https://www.w3.org/TR/xmlschema-2/#boolean"
}
},
"access": true
},
"stylesheet": {
"jsonld": "flattened"
},
"expected": [{
"@id": "_:b0",
"https://schema.org/isAccessibleForFree": [{
"@type": "https://www.w3.org/TR/xmlschema-2/#boolean",
"@value": true
}]
}]
}
Examples
See unit tests : https://github.com/Inist-CNRS/node-jbj-jsonld/tree/master/test
Try it
http://Inist-CNRS.github.io/jbj-playground/
(don't forget to click on JSON-LD button -- when it will exist)