jsonld.js
jsonld.js copied to clipboard
flatten only lets through @id and @type?
I'm a newby at json-ld but not sure why the following happens when I call jsonld.flatten:
Run this script:
const jsonld = require('jsonld');
const obj = {
"@id": "http://store.example.com/",
"@type": "Store",
"name": "Links Bike Shop",
"description": "The most \"linked\" bike store on earth!"
};
jsonld.flatten(obj).then(x => {
console.log(x);
});
Expected output:
[ { '@id': 'http://store.example.com/', '@type': [ 'Store' ], name: [ 'Links Bike Shop' ], description: [ 'The most "linked" bike store on earth!' ] } ]
Actual output:
[ { '@id': 'http://store.example.com/', '@type': [ 'Store' ] } ]
You probably want to have a @context that defines what the "name" and "description" terms are. You need that in the source data so it can be expanded. And also as a flatten param if you want the shorter terms in the output.
const jsonld = require('jsonld');
const obj = {
"@context": "http://schema.org/",
"@id": "http://store.example.com/",
"@type": "Store",
"name": "Links Bike Shop",
"description": "The most \"linked\" bike store on earth!"
};
const ctx = {
"@context": "http://schema.org/"
};
jsonld.flatten(obj, ctx).then(x => {
console.log(JSON.stringify(x, null, 2));
});
{
"@context": "http://schema.org/",
"@graph": [
{
"id": "http://store.example.com/",
"type": "Store",
"description": "The most \"linked\" bike store on earth!",
"name": "Links Bike Shop"
}
]
}
Could we use flatten method at frontend environment?