nodejs - 从 jsonl 文件访问提取的数据

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

我正在使用 chrome 扩展程序,它可以从网站中提取数据,并使我能够将它们导出为 jsonl 格式或 csv。

我需要将导出的文件转换成 xls,我找到了一个很好的节点模块来实现这个,我现在的问题是 initail jsonl 文件的解析。

我正在使用这段代码逐行读取文件

import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import rl from 'readline'
import exportJSON from 'export-from-json'


const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

const sourceFile = path.format({
  dir: __dirname,
  base: 'sitemap.jsonl'
})

const fileStream = fs.createReadStream(sourceFile)
const readline = rl.createInterface({
  input: fileStream,
  crlfDelay: Infinity
})

let products = []

for await (const line of readline){
  products.push(line)
}

由于 initail 格式是 jsonl,我只是将单行数据推送到数组中。我的问题是当我尝试使用点表示法访问每个单个对象的属性时,我尝试循环数组并且它工作正常,但我无法获得我需要的信息并且我无法然后使用从 json 库导出的文件创建一个 xls 文件。

输入文件的每一行都会有这个结构

{"info1":"detail", "info2":"detail", "info3": ["xys"], "info4": ["123456"]}

我怎样才能得到我需要的数据??

node.js arrays json
© www.soinside.com 2019 - 2024. All rights reserved.