使用nodejs解析结构化JSON-LD文件的问题

问题描述 投票:1回答:1

我尝试使用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": ""
  }
}

感谢您的帮助!蒂埃里

node.js semantic-web json-ld
1个回答
0
投票

您正在使用的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.org/",
        "bd": "http://www.bigdata.com/rdf#",
        (...)
     },
     "@id": "https://data.datatourisme.gouv.fr/3/06a7f439-3e02-3aa2-8301-f850bb5b792f"
}

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

© www.soinside.com 2019 - 2024. All rights reserved.