Skip to content

Instantly share code, notes, and snippets.

@1kohei1
Created August 2, 2015 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1kohei1/80806b9eef17c9d7b140 to your computer and use it in GitHub Desktop.
Save 1kohei1/80806b9eef17c9d7b140 to your computer and use it in GitHub Desktop.
Compare two JSON
var prod = {
a: 1,
b: 2,
c: {
id: 1,
name: '1kohei1',
address: 'USA'
}
}
var dev = {
a: 1,
b: 2,
c: {}
}
var objectKeys = {};
var excludedKeys = [];
console.log('---START---');
compareTwoJSON(prod, dev, '', objectKeys, excludedKeys);
/**
* Print out diff of two JSON
* @param jsonA The correct JSON
* @param jsonB JSON you want to check diff
* @param keys Necessary for the function. Just pass ''
* @param objectKeys Objects to find unique objects in array.
* @param excludedKeys Array of keys that doesn't have to be checked.
*/
function compareTwoJSON(jsonA, jsonB, keys, objectKeys, excludedKeys) {
for (var key in jsonA) {
if (jsonB.hasOwnProperty(key)) {
if (jsonA[key] === null && jsonB[key] === null) {
// Ignore null first because typeof null is object
} else if (typeof jsonA[key] === typeof jsonB[key]) { // Check the data type
if (Array.isArray(jsonA[key])) { // If the data type is array
keys = '[' + key + ']';
var jsonAArray = jsonA[key];
var jsonBArray = jsonB[key];
for (var i = 0; i < jsonAArray.length; i++) { // Find the same item in jsonB[key] by iterating jsonA[key]
var jsonAItem = jsonAArray[i];
var jsonBItem = '';
var objectKey = objectKeys[key];
if (typeof jsonAItem === 'object') {
// If it is an array of objects, find the equal object by property
jsonBItem = _.find(jsonBArray, function(obj) {
return obj[objectKey] === jsonAItem[objectKey];
});
} else {
jsonBItem = _.find(jsonBArray, function(obj) {
return obj === jsonAItem;
});
}
if (jsonBItem) { // Check if the same item is found in jsonB[key]
if (keys && keys.indexOf('.') >= 0) {
keys = keys.split('.').splice(0, 1);
}
keys += '.' + jsonAItem[objectKey];
compareTwoJSON(jsonAItem, jsonBItem, keys, objectKeys, excludedKeys); // Iterate again to compare item in jsonA and jsonB
} else {
var missingItem = typeof jsonAItem === 'object' ? jsonAItem[objectKey] : jsonAItem;
console.log('1. Dev is missing [' + key + '].' + missingItem);
}
}
} else if (typeof jsonA[key] === 'object') {
if (keys && keys.indexOf('.') >= 0) {
keys = keys.split('.').splice(0, 1);
}
var objectKey = objectKeys[key];
keys += _.isUndefined(objectKey) ? key : objectKey;
compareTwoJSON(jsonA[key], jsonB[key], keys, objectKeys, excludedKeys);
} else if (!_.isEqual(jsonA[key], jsonB[key])) {
if (excludedKeys.indexOf(key) === -1) {
console.log('2. Dev has different value at ' + keys + '.' + key);
}
}
} else {
console.log('3. Dev has different data type at ' + keys + '.' + key);
}
} else {
console.log('4. Dev is missing property ' + keys + '.' + key);
}
}
}
console.log('---FINISH---');
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment