I have json that looks like this
const myJson = [
{
"title":"1. Lockdown Population by Loan (186006)",
children:[
{
title:"1.1 Previous (191225)",
"firstLevelSubNodes:[
{
title:"1.1.1 Roll forward (178260)" ,
"firstLevelSubNodes":[{
title:"1.1.1.1 Roll forward (178260)"
}]
}
],
"secondLevelSubNodes":[
title:"1.2.1 Bounce forward (178260)" ,
"firstLevelSubNodes":[{
title:"1.2.1.1 Dash forward (178260)",
"firstLevelSubNodes":[{
title:"1.2.1.1.1 Skip forward (178260)",
}]
}]
],
"thirdLevelSubNodes":[
],
"fourthLevelSubNodes":[
]
}
],
}
]
Everywhere you see the word xxxLevelSubNodes it needs to say the word "children" like below.
const myJson = [
{
"title":"1. Lockdown Population by Loan (186006)",
children:[
{
title:"1.1 Previous (191225)",
"children:[
{
title:"1.1.1 Roll forward (178260)" ,
"children":[{
title:"1.1.1.1 Roll forward (178260)"
}]
}
],
"children":[
title:"1.2.1 Bounce forward (178260)" ,
"children":[{
title:"1.2.1.1 Dash forward (178260)",
"children":[{
title:"1.2.1.1.1 Skip forward (178260)",
}]
}]
],
"children":[
],
"children":[
]
}
],
}
]
I have tried to loop through but it renames the word with children however it overwrites the last key. Also it doesn't loop through the nested portion of the JSON. I need it to remain in the exact same structure as above
const res = { firstLevelSubNodes: 'children', secondLevelSubNodes: 'children',
thirdLevelSubNodes: 'children', fourthLevelSubNodes: 'children' };
const object = JSON.parse(myJson);
for (const k in res) {
const newValue = res[k];
object[newValue] = object[k];
object[newValue].name = newValue;
delete object[k];
}
childrenlist and add all the xxxLevelSubNodes to itJSON.stringify()and then use some regex withstring.replace()You would get the result you're describing.childrenarrays for everything that matches?