gpt4 book ai didi

json - 合并目录nodejs中的所有json文件

转载 作者:搜寻专家 更新时间:2023-11-01 00:21:35 29 4
gpt4 key购买 nike

我想将所有 json 文件合并到 nodejs 的一个目录中。这些文件由用户上传,我只知道它们的名称是 device"count".json。计数每次递增。我知道 json-concat,但如何使用它合并目录中的所有文件?

    jsonConcat({
src: [],
dest: "./result.json"
}, function (json) {
console.log(json);
});

最佳答案

您可以使用fs 模块读取目录中的文件,然后将它们传递给json-concat

const jsonConcat = require('json-concat');
const fs = require('fs');

// an array of filenames to concat
const files = [];

const theDirectory = __dirname; // or whatever directory you want to read
fs.readdirSync(theDirectory).forEach((file) => {
// you may want to filter these by extension, etc. to make sure they are JSON files
files.push(file);
})

// pass the "files" to json concat
jsonConcat({
src: files,
dest: "./result.json"
}, function (json) {
console.log(json);
});

关于json - 合并目录nodejs中的所有json文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40772428/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com