jsonld.js icon indicating copy to clipboard operation
jsonld.js copied to clipboard

flatten only lets through @id and @type?

Open michielbdejong opened this issue 5 years ago • 2 comments

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' ] } ]

michielbdejong avatar Oct 20 '20 13:10 michielbdejong

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"
    }
  ]
}

davidlehn avatar Mar 18 '21 06:03 davidlehn

Could we use flatten method at frontend environment?

Tsung-Jen avatar Jul 02 '22 07:07 Tsung-Jen