All Projects → joshswan → gulp-merge-json

joshswan / gulp-merge-json

Licence: MIT license
A gulp plugin to merge JSON & JSON5 files into one file

Programming Languages

javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to gulp-merge-json

Gulp Ftp
[DEPRECATED] Upload files to an FTP-server
Stars: ✭ 100 (+185.71%)
Mutual labels:  gulp, gulp-plugin
Glup
Some of the gulp tutorial -《gulp笔记》
Stars: ✭ 136 (+288.57%)
Mutual labels:  gulp, gulp-plugin
Gulp Flatten
Gulp plugin: remove or replace relative paths for files
Stars: ✭ 102 (+191.43%)
Mutual labels:  gulp, gulp-plugin
Gulp Develop Server
Development assistant for node.js server by gulp
Stars: ✭ 72 (+105.71%)
Mutual labels:  gulp, gulp-plugin
gulp-xo
Validate files with XO
Stars: ✭ 37 (+5.71%)
Mutual labels:  gulp, gulp-plugin
Gulp Plugin Boilerplate
Boilerplate to kickstart creating Gulp plugins
Stars: ✭ 74 (+111.43%)
Mutual labels:  gulp, gulp-plugin
Gulp Bro
👊 gulp + browserify + incremental build, done right.
Stars: ✭ 119 (+240%)
Mutual labels:  gulp, gulp-plugin
Gulp Jsonlint
🔍 jsonlint plugin for Gulp
Stars: ✭ 26 (-25.71%)
Mutual labels:  gulp, gulp-plugin
gulp-upload-qcloud
腾讯云 cos 静态资源上传 gulp 插件
Stars: ✭ 18 (-48.57%)
Mutual labels:  gulp, gulp-plugin
Gulp Html Replace
Replace build blocks in HTML. Like useref but done right.
Stars: ✭ 222 (+534.29%)
Mutual labels:  gulp, gulp-plugin
Gulp Json Editor
A gulp plugin to edit JSON objects
Stars: ✭ 55 (+57.14%)
Mutual labels:  gulp, gulp-plugin
gulp-sitemap
Generate a search engine friendly sitemap.xml using a Gulp stream
Stars: ✭ 60 (+71.43%)
Mutual labels:  gulp, gulp-plugin
Gulp Require Tasks
Splits Gulpfile into multiple individual files
Stars: ✭ 51 (+45.71%)
Mutual labels:  gulp, gulp-plugin
Gulp Ngmin
[DEPRECATED] Pre-minify AngularJS apps with ngmin
Stars: ✭ 89 (+154.29%)
Mutual labels:  gulp, gulp-plugin
Generator Gulp Plugin Boilerplate
Scaffold out a Gulp plugin boilerplate
Stars: ✭ 46 (+31.43%)
Mutual labels:  gulp, gulp-plugin
Gulp Modernizr
Gulp wrapper for custom Modernizr builds
Stars: ✭ 111 (+217.14%)
Mutual labels:  gulp, gulp-plugin
Gulp Pug
Gulp plugin for compiling Pug templates
Stars: ✭ 512 (+1362.86%)
Mutual labels:  gulp, gulp-plugin
Gulp Angular Templatecache
Concatenates and registers AngularJS templates in the $templateCache.
Stars: ✭ 530 (+1414.29%)
Mutual labels:  gulp, gulp-plugin
Gulp Tap
Easily tap into a gulp pipeline without creating a plugin.
Stars: ✭ 158 (+351.43%)
Mutual labels:  gulp, gulp-plugin
gulp-sort
Sort files in stream by path or any custom sort comparator
Stars: ✭ 22 (-37.14%)
Mutual labels:  gulp, gulp-plugin

gulp-merge-json

Version Downloads Build Status Coverage License

A gulp plugin for deep-merging multiple JSON files into one file. Export as JSON or a node module.

Usage

gulp.src('jsonFiles/**/*.json')
	.pipe(merge(options))
	.pipe(gulp.dest('./dist'));

Options

Key Type Default Description
fileName String combined.json Output filename
edit Function json => json Edit function (add/remove/edit keys during merge)
transform Function json => json Transform final merged object (similar to edit but applied at the end)
startObj Object/Array {} Starting object to merge into (useful for providing default values)
endObj Object/Array Object to merge after file merging complete (useful for overwriting with special values)
exportModule Boolean/String false Output module.exports = {MERGED_JSON_DATA}; or {exportModule} = {MERGED_JSON_DATA} when string passed
concatArrays Boolean false Whether to concatenate arrays instead of merging
mergeArrays Boolean true Whether to merge arrays or overwrite completely
customizer Function Custom merge function for use with mergeWith
jsonReplacer Function Custom JSON replacer function passed to stringify
jsonSpace String \t String used for white space by stringify
json5 Boolean false Use JSON5 instead of JSON for parse and stringify

Examples

var merge = require('gulp-merge-json');

/**
 * Basic functionality
 */
gulp.src('jsonFiles/**/*.json')
	.pipe(merge())
	.pipe(gulp.dest('./dist'));

/**
 * Edit JSON with function
 */
gulp.src('jsonFiles/**/*.json')
	.pipe(merge({
		fileName: 'file.json',
		edit: (parsedJson, file) => {
			if (parsedJson.someValue) {
				delete parsedJson.otherValue;
			}

			return parsedJson;
		},
	}))
	.pipe(gulp.dest('./dist'));

/**
 * Edit final JSON with transformer function
 */
gulp.src('jsonFiles/**/*.json')
	.pipe(merge({
		fileName: 'file.json',
		transform: (mergedJson) => {
			return {
				key: {
					type: 'data',
					...mergedJson,
				};
			};
		},
	}))
	.pipe(gulp.dest('./dist'));

/**
 * Provide a default object (files are merged in order so object values will be overwritten)
 */
gulp.src('jsonFiles/**/*.json')
	.pipe(merge({
		startObj: { someKey: 'defaultValue' },
	}))
	.pipe(gulp.dest('./dist'));

/**
 * Provide an overwriting object (merged at the end)
 */
gulp.src('jsonFiles/**/*.json')
	.pipe(merge({
		endObj: { someKey: 'specialValue' },
	}))
	.pipe(gulp.dest('./dist'));

/**
 * Output module.exports = {JSON_DATA}
 */
gulp.src('jsonFiles/**/*.json')
	.pipe(merge({
		exportModule: true,
	}))
	.pipe(gulp.dest('./dist'));

/**
 * Output a custom variable = {JSON_DATA}
 */
gulp.src('jsonFiles/**/*.json')
	.pipe(merge({
		fileName: 'dataModule.js',
		exportModule: 'const myVar',
	}))
	.pipe(gulp.dest('./dist'));

/**
 * Provide replacer and space options for JSON.stringify
 */
gulp.src('jsonFiles/**/*.json')
    .pipe(merge({
        jsonSpace: '  ',
        jsonReplacer: (key, value) => {/*...*/}
    })
    .pipe(gulp.dest('./dist'));

/**
 * Use a customizer function for custom merging behavior
 */
gulp.src('jsonFiles/**/*.json')
  .pipe(merge({
    customizer: (objA, objB) => {
      // Example: Concat arrays but only keep unique values
      if (Array.isArray(objA) && Array.isArray(objB)) {
        return objA.concat(objB).filter((item, index, array) => (
          array.indexOf(item) === index
        ));
      }

      return undefined;
    },
  }))
  .pipe(gulp.dest('./dist'));

/**
 * JSON5
 */
gulp.src('jsonFiles/**/*.json5')
	.pipe(merge({
		json5: true,
	}))
	.pipe(gulp.dest('./dist'));

Example Input

/*
	json/defaults.json
 */
{
	"key1": {
		"data1": "value1",
		"data2": "value2"
	},
	"key2": {
		"dataA": "valueA",
		"dataB": {
			"a": "b",
			"c": "d"
		}
	}
}

/*
	json/development.json
 */
{
	"key1": {
		"data1": "devValue"
	},
	"key2": {
		"dataB": {
			"c": "DEV MODE!"
		}
	},
	"key3": {
		"important": "value"
	}
}

Example Output

/*
	dist/combined.json
 */
{
	"key1": {
		"data1": "devValue",
		"data2": "value2"
	},
	"key2": {
		"dataA": "valueA",
		"dataB": {
			"dataA": "valueA",
			"dataB": {
				"a": "b",
				"c": "DEV MODE!"
			}
		}
	},
	"key3": {
		"important": "value"
	}
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].