digitalbazaar / jsonld.js

A JSON-LD Processor and API implementation in JavaScript
https://json-ld.org/
Other
1.66k stars 195 forks source link

flatten only lets through @id and @type? #417

Open michielbdejong opened 3 years ago

michielbdejong commented 3 years ago

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' ] } ]
davidlehn commented 3 years ago

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"
    }
  ]
}
Tsung-Jen commented 2 years ago

Could we use flatten method at frontend environment?