I have JSON data with nested objects. I want to remove the "id" from this structure and return the changed JSON from this function. I tried to do this recursively below, but could not return the modified JSON.
var jsonStr =
{"_id":"7r0c0342e",
"user":"myuser",
"project":"abcd",
"info":{"DOMAIN":{"Department":{"profile":[{"workex":8,"name":"alex","id":82838},
{"workex":8,"name":"smith","id":84838} ]}}} };
processJSON(jsonStr);
function processJSON(jsondata) {
for (var i in jsondata) {
var row = jsondata[i];
if(typeof row == "object") {
processJSON(row);
} else if(typeof row == 'number') {
if(i == 'id') {
delete jsondata[i];
} else {
continue;
}
} else {
continue;
}
}
}
console.log(jsonStr);
How can I return the rest of the JSON from processJSON () and hold it in a variable? Secondly, is this the right way to do this recursively?
Thank.
source
share