How to use the jsonld.flatten function in jsonld

To help you get started, we’ve selected a few jsonld examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github digitalbazaar / vc-js / tests / 10-verify.spec.js View on Github external
async function _pluckDidNode(did, target, didDocument) {
  // flatten to isolate target
  jsonld.documentLoader = testLoader.documentLoader.bind(testLoader);
  const flattened = await jsonld.flatten(didDocument);
  // filter out non-DID nodes and find target
  let found = false;
  const filtered = [];
  for(const node of flattened) {
    const id = node['@id'];
    if(id === target) {
      filtered.push(node);
      found = true;
      break;
    }
  }
  // target not found
  if(!found) {
    const err = new Error('Not Found');
    err.httpStatusCode = 404;
    err.status = 404;
github apache / incubator-streampipes / src / app / platform-services / tsonld / tsonld.js View on Github external
return Observable_1.Observable.fromPromise(new Promise(function (resolve, reject) {
            jsonld.flatten(obj, context, function (err, data) {
                // console.log('flatten data: bla bla bla: ' + JSON.stringify(data, null, 2));
                resolve(data);
            });
        }));
    };
github DefinitelyTyped / DefinitelyTyped / types / jsonld / jsonld-tests.ts View on Github external
});

jsonld.expand(doc)
.then((res) => {
    log(res);
});

jsonld.expand(doc, {keepFreeFloatingNodes: false})
.then((res) => {
    log(res);
});

/**
 * flatten() test
 */
jsonld.flatten(doc, context, (err, res) => {
    log(res);
});

jsonld.flatten(doc, context, {expandContext: context}, (err, res) => {
    log(res);
});

jsonld.flatten(doc, context)
.then((res) => {
    log(res);
});

jsonld.flatten(doc, context, {base: baseUrl})
.then((res) => {
    log(res);
});
github sballesteros / dcat / index.js View on Github external
if (arguments.length === 3) {
    callback = opts;
    opts = {};
  } else {
    opts = clone(opts);
  }

  //flatten the doc to get all the @id and generate opts.reservedIds
  var tdoc = clone(doc);
  var ctx;
  if (tdoc['@context'] === SchemaOrgIo.contextUrl) {
    tdoc['@context'] = SchemaOrgIo.context()['@context']; //offline
    ctx = tdoc['@context'];
  }

  jsonld.flatten(tdoc, ctx || tdoc['@context'], function(err, fdoc){
    if (err) return callback(err);

    var ioIds = fdoc['@graph']
      .filter(function(x){
        return x['@id'] && x['@id'].split(':')[0] === 'ldr';
      })
      .map(function(x){
        return x['@id'].split(':')[1]
      });

    opts.reservedIds = opts.reservedIds || {};
    ioIds.forEach(function(id){ opts.reservedIds[id] = true; });

    //get namespace
    if (doc['@id']) {
      opts.namespace = opts.namespace || this.namespace(doc['@id']);
github jupyterlab / jupyterlab-metadata-service / src / linked_data_registry.ts View on Github external
return async () =>
      findEntity(
        (await expand(
          await flatten(await Promise.all(thunks.map(t => t())), null)
        )) as Array,
        url
      )!;
  }
github sballesteros / dcat / index.js View on Github external
function _next(err, pdoc) {
      if (err) return callback(err);

      //reverse @context transfo
      if (ctx && pdoc['@context'] === ctxUrl) {
        pdoc['@context'] = ctx;
      }

      callback(null, pdoc);
    };

    if (opts.normalize) {
      jsonld.normalize(doc, {format: 'application/nquads'}, _next);
    } else if (opts.profile === 'flattened') {
      jsonld.flatten(doc, doc['@context'], _next);
    } else if (opts.profile === 'expanded') {
      jsonld.expand(doc, {expandContext: doc['@context']}, _next);
    } else {
      jsonld.compact(doc, doc['@context'], _next);
    }

  }.bind(this));
};
github linkeddata / rdflib.js / src / jsonldparser.js View on Github external
export default function jsonldParser (str, kb, base, callback) {
  const baseString = base && Object.prototype.hasOwnProperty.call(base, 'termType')
    ? base.value
    : base

  return jsonld
    .flatten(JSON.parse(str), null, { base: baseString })
    .then((flattened) => flattened.reduce((store, flatResource) => {
      const id = flatResource['@id']
        ? kb.rdfFactory.namedNode(flatResource['@id'])
        : kb.rdfFactory.blankNode()

      for (const property of Object.keys(flatResource)) {
        if (property === '@id') {
          continue
        }
        const value = flatResource[property]
        if (Array.isArray(value)) {
          for (let i = 0; i < value.length; i++) {
            kb.addStatement(kb.rdfFactory.quad(id, kb.rdfFactory.namedNode(property), jsonldObjectToTerm(kb, value[i])))
          }
        } else {
github dpjanes / node-iotdb / obsolete / graph.js View on Github external
GraphManager.prototype.load_jsonld = function (jd) {
    var self = this;

    jsonld.flatten(jd, function (error, robjectds) {
        self._load_flattened(error, jd, robjectds);
    });
};
github jupyterlab / jupyterlab-metadata-service / src / index.tsx View on Github external
return async () =>
      findEntity(
        (await expand(
          await flatten(await Promise.all(thunks.map(t => t())), null)
        )) as Array,
        url
      )!;
  }