My JSON sample looks like this:
var obj={
"results":{
"grade":"A",
"marks":12
},
"data":{
"name":"sam",
"gender":"male",
"age":10
}
};
I am trying to transform the above JSON to:
var obj={
"results":{
"grade":"A",
"marks":12
},
"name":"sam",
"age":10
}
I attempted to achieve this using a forEach loop:
for(var exKey in obj) {
if(exKey=='data'){
//replace key data with its value
}
}
Unfortunately, I couldn't figure out the logic needed to accomplish this. Can anyone provide assistance on how to achieve this? Thank you for your responses.
If dealing with more complex JSON like:
var obj={
"results":{
"grade":"A",
"res":"fail",
"marks":12
},
"data":{details:{"name":"sam",
"gender":"male",
"age":10
}
},
"feespaid":"yes",
"joindate":"sunday"
};
In a scenario where I need to modify or delete keys within a nested structure, such as:
var obj={
"results":{
"grade":"A",
"marks":12
},
"data":{details:{"name":"sam",
"age":10
}
},
"joindate":"sunday"
};
How can we traverse through the JSON and perform deletions?