Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using gulp to combine and edit multiple json files into one

Tags:

json

gulp

I'm a little bit into gulp and I'm looking for the right combination of tasks to read n amount of similarly formatted json files, pull out a specific array from each, combine them, and save as a new file. I've successfully used gulp-concat to combine files, and gulp-json-editor to change values on one file, but I'm having a hard time wrapping my head around how to string the processes together.

n number of json files formatted as follows:

{
    "id": "groupofthings1",
    "things": [
        { ... },
        { ... },
    ],
    ...
}

Desired single file output containing all "things":

[
    { ... },
    { ... },
    { ... },
    { ... },
    ...
]
like image 927
pleeschultz Avatar asked Jun 26 '14 13:06

pleeschultz


1 Answers

Took me a bit of searching, but ended up finding jsoncombine

It loads all json files and runs them through a custom function as a hash of json objects. Each json object is saved under its original files name.

gulp.src("./groups/of/things/**/things.json")
.pipe(jsoncombine("all-things.json",function(data){
    // do any work on data here
    return new Buffer(JSON.stringify(data));
 }))
 .pipe(gulp.dest("./dest"));
like image 122
pleeschultz Avatar answered Oct 13 '22 01:10

pleeschultz