Skip to content

Instantly share code, notes, and snippets.

@benstr
Created November 4, 2014 06:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benstr/f61301e7fc55530253de to your computer and use it in GitHub Desktop.
Save benstr/f61301e7fc55530253de to your computer and use it in GitHub Desktop.
Node script to deep merge JSON files

Merge JSON Files

I am a MeteorJS developer and I use MeteorKitchen a lot. It helps get an MVP up and going in no time!

There is just one problem, with fairly large apps the the JSON constructor file becomes huge and unmanagable (especially since you cannot comment JSON code).

I desired to break my JSON into modules and let the computer handle merging them. The attached script deep merges my JSON and allows me to specify the order to merge my JSON files. Perfect.

Modify as needed then from the terminal run $ node mergeJSON.js

var merge = require('deepmerge');
var fs = require('fs');
var a = require('./json/a-start.json');
var b = require('./json/collections.json');
var c = require('./json/z-sample-fields.json');
var merge1 = merge(a, b);
var merge2 = merge(merge1, c);
var outputFilename = 'json/output.json';
fs.writeFile(outputFilename, JSON.stringify(merge2, null, 2), function(err) {
if(err) {
console.log(err);
} else {
console.log("JSON saved to " + outputFilename);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment