user7201227 user7201227 - 15 days ago 5
JSON Question

Merge all json files in a directory nodejs

I would like to merge all json files in a directory in nodejs. The files are being uploaded by user and all i]I know is their names are device"count".json. Count being incrememented each time. I know of json-concat but how do I use it to merge all files in a directory?

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

Answer

if You read docs carefully You'll see this part:

The options object passed may have the following keys:

src:
    (String) path pointing to a directory
    (Array) array of paths pointing to files and/or directories
    defaults to . (current working directory)
    Note: if this is a path points to a single file, nothing will be done.

so here is fix:

1) move json files to some concrete path.

2) check this code:

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

and here is the prove how it uses src param

Mostly it needs from developer not just to use 3-rd part packages, but also dive into it's sources.

in short: KISS (: