I have a JSON which looks like this:-
{
"property" : value,
"property" : value,
"subProperty" : {
"subProperty1" : {
"subProperty1a" : {
"property" : value,
"property" : value
},
"subProperty1b" : {
"property" : ["1", "2"],
"property" : value
},
.....
},
"subProperty2" : {
"subProperty2a" : {
"property" : value,
"property" : value
},
"subProperty2b" : {
"property" : value,
"property" : value
},
.....
},
"subProperty3" : {
"subProperty3a" : {
"property" : value,
"property" : value
},
"subProperty3b" : {
"property" : value,
"property" : value
},
.....
}
}
}
This JSON can have any number of fields added later on in any position.What I want is when I add new fields, then I want to compare old JSON with new JSON. If any propert is missing, bind that property with new one.
Right now I am using following code to achieve this:-
scope.tempObj = {};
scope.findObjectByLabel = function(obj1, obj2, obj3){
for(var i in obj1)
{
obj3[i] = obj2[i];
if(obj2[i] == undefined)
{
obj3[i] = obj1[i];
}
if(typeof(obj1[i]) == 'object')
{
scope.findObjectByLabel(obj1[i], obj2[i],obj3[i]);
}
}
return obj3;
}
scope.newJSON = scope.findObjectByLabel(scope.newJSON, scope.oldJSON ,scope.tempObj);
But the above code can only compare through one level. Can any one suggest me any improvement on the above code so that it can compare through entire JSON.