joshswan / gulp-merge-json

A gulp plugin to merge JSON & JSON5 files into one file

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mergeArrays: false

PaulieScanlon opened this issue · comments

Hey. I'm using gulp-merge-json and it's great, so thanks but....

I'm using mergeArrays:false and my outputted Json contains key values that have been deleted from the source Json.

Am i correct in thinking mergeArrays:false should completely delete what was in the combined file then merge all Json, then re-create the Json object?

This is my gulp task:

gulp.task('combine-json', () => {
gulp.src('./src/js/data/*.json')
.pipe(merge({
fileName: '_combined.json',
mergeArrays: false
}))
.pipe(gulp.dest('./src/js/json'))
})

If i delete a key value from one of the Json files in ./src/js/data/ the key value still appears in my _combined.json

Am i doing something wrong or have i misunderstood mergeArrays: false?

Thanks in advance!

The standard behavior is to merge keys from different files into one file, overwriting where duplicate keys are encountered. Example:

// File 1
{
  "key1": "value1",
  "key2": "value2"
}

// File 2
{
  "key2": "overwrite",
  "key3": "value3"
}

// Combined
{
  "key1": "value1",
  "key2": "overwrite",
  "key3": "value3"
}

The mergeArrays option affects how the library handles array values specifically. Say you have an array at the same key in two files. The default behavior would result in the second array being in the output at that key. mergeArrays allows you to specify that you want those arrays combined instead. Example:

// File 1
{
  "key1": ["value1"]
}

// File 2
{
  "key1": ["value2"]
}

// Combined WITHOUT mergeArrays
{
  "key1": ["value2"]
}

// Combined WITH mergeArrays
{
  "key1": ["value1", "value2"]
}

Also note the new customizer option that allows you to write your own function specifying how you want to merge your data.