最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

使用nodejs解析结构化JSON

网站源码admin18浏览0评论

使用nodejs解析结构化JSON

使用nodejs解析结构化JSON

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

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

{
  "@context": {
    "@vocab": "",
    "schema": "/",
    "bd": "",
    (...)
  },
  "@graph": [{
    "@id": "",
    "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": ""
  },
  "predicate": {
    "value": ".1/date"
  },
  "object": {
    "value": "2013-10-30",
    "datatype": {
      "value": ""
    },
   "language": ""
  },
  "graph": {
    "value": ""
  }
}

感谢您的帮助!蒂埃里

回答如下:

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

我认为您想要从描述中获得的是JsonLd图的frame。您可以使用标准的JsonLd parsing library for Node来实现。

在您的情况下,框架应尽可能简单

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

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论