Skip to content

Instantly share code, notes, and snippets.

@mintyPT
Created November 2, 2018 14:09
Show Gist options
  • Save mintyPT/da56f9c6cd47d0d96f90e4f446a9030e to your computer and use it in GitHub Desktop.
Save mintyPT/da56f9c6cd47d0d96f90e4f446a9030e to your computer and use it in GitHub Desktop.
Shows the difference (in keys) between 2 json files
// node diff.js f1.json f2.json
const _ = require('lodash');
const path = require('path');
if (process.argv.length != 4) {
throw new Error('node script.js file1.json file2.json');
}
const f1 = process.argv[2];
const j1 = require(path.resolve(f1)); // eslint-disable-line
const f2 = process.argv[3];
const j2 = require(path.resolve(f2)); // eslint-disable-line
const diff1 = _.difference(_.keys(j1), _.keys(j2));
const diff2 = _.difference(_.keys(j2), _.keys(j1));
const obj1 = _.chain(diff1).map(k => [k, j1[k]]).fromPairs().value();
const obj2 = _.chain(diff2).map(k => [k, j2[k]]).fromPairs().value();
console.log('▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾'); // eslint-disable-next-line
console.log(`Keys only in ${f1}: ${_.join(diff1, ', ')}`); // eslint-disable-next-line
console.log(JSON.stringify(obj1, null, 4)); // eslint-disable-next-line
console.log('▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾▾'); // eslint-disable-next-line
console.log(`Keys only in ${f2}: ${_.join(diff2, ', ')}`); // eslint-disable-next-line
console.log(JSON.stringify(obj2, null, 4)); // eslint-disable-next-line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment