【问题标题】:Problems to parse structured JSON-LD file with nodejs使用 nodejs 解析结构化 JSON-LD 文件的问题
【发布时间】:2019-09-24 07:32:22
【问题描述】:

我尝试使用 jsonld-streaming-parser.js 库通过 Nodejs 解析 JSON-LD 文件。

该文件是结构化的 JSON-LD,因此我感兴趣的所有元素都位于 @graph 的根目录下:

{
  "@context": {
    "@vocab": "https://www.datatourisme.gouv.fr/ontology/core#",
    "schema": "http://schema.org/",
    "bd": "http://www.bigdata.com/rdf#",
    (...)
  },
  "@graph": [{
    "@id": "https://data.datatourisme.gouv.fr/3/06a7f439-3e02-3aa2-8301-f850bb5b792f",
    "dc:date": [{
      "@value": "2013-10-30",
      "@type": "xsd:date"
    },{
      "@value": "2019-08-30",
      "@type": "xsd:date"
    }],
    "dc:identifier": "eudonet:52945",
    "@type": ["schema:Landform","NaturalHeritage","PlaceOfInterest","PointOfInterest","urn:resource"],
    "rdfs:label": {
      "@value": "L'arbre du Pied Cornier",
      "@language": "fr"
    },
    (...)
  }]
}

我可以使用以下代码解析文件:

const JsonLdParser = require('jsonld-streaming-parser').JsonLdParser;

const parser = new JsonLdParser();

const getStream = () => {
  const jsonData = 'flux-5339-201909240851.partial.jsonld';
  const stream = fs.createReadStream(jsonData, {encoding: 'utf8'});
  return stream.pipe(parser);
};

getStream()
  .on('data', (data) => {
    console.log('data = ', data);
  })
  .on('error', () => {
    console.error(error);
  })
  .on('end', () => {
    console.log('All triples were parsed!');
  });

我希望在data 回调中包含一个元素的全面内容,但得到了这个:

{
  "subject": {
    "value": "https://data.datatourisme.gouv.fr/3/06a7f439-3e02-3aa2-8301-f850bb5b792f"
  },
  "predicate": {
    "value": "http://purl.org/dc/elements/1.1/date"
  },
  "object": {
    "value": "2013-10-30",
    "datatype": {
      "value": "http://www.w3.org/2001/XMLSchema#date"
    },
   "language": ""
  },
  "graph": {
    "value": ""
  }
}

感谢您的帮助! 蒂埃里

【问题讨论】:

  • comprehensive content 是什么意思?
  • 我的意思是我在文件中的所有元素字段。例如:标签、类型、日期……感谢您的留言!

标签: node.js semantic-web json-ld


【解决方案1】:

您正在使用的 JsonLd 流解析器库似乎仅将 JsonLd 转换为 RDF 三元组(有关详细信息,请参阅RDF spec)。

我认为你想要从你的描述中得到一个 JsonLd 图的frame。您可以使用标准 JsonLd parsing library for Node 来实现这一点。

在你的情况下,框架应该很简单

{
    "@context": {
        "@vocab": "https://www.datatourisme.gouv.fr/ontology/core#",
        "schema": "http://schema.org/",
        "bd": "http://www.bigdata.com/rdf#",
        (...)
     },
     "@id": "https://data.datatourisme.gouv.fr/3/06a7f439-3e02-3aa2-8301-f850bb5b792f"
}

它应该为您提供紧凑 JsonLd 中第一项的属性。如果图表中有多个项目,您可以将 @id 更改为 @type 并返回与特定类型匹配的所有项目。

【讨论】:

    猜你喜欢
    • 2015-05-28
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 2020-02-22
    • 2015-08-26
    • 2015-08-29
    相关资源
    最近更新 更多