Here is a JSON
Object that I am working with:
{
"status": "CREATED",
"overrides": {
"name": "test_override"
},
"package_name": "test",
"name": "app1",
"defaults": {
"job": {
"example": {
"executors_num": "2",
"freq_in_mins": "180",
"executors_mem": "256M",
"job_name": "batch_example"
}
}
}
}
The goal is to transform the above JSON
object into a structure like this. Essentially, adding an array to each nested object:
{
"children": [
{
"status": "CREATED"
},
{
"obj": "overrides",
"children": [
{
"name": "test_override"
}
]
},
{
"package_name": "test"
},
{
"name": "app1"
},
{
"obj": "defaults",
"children": [
{
"obj": "job",
"children": [
{
"obj": "example",
"children": [
{
"executors_num": "2",
"freq_in_mins": "180",
"executors_mem": "256M",
"job_name": "batch_example"
}]
}]
}]
}
]
}
Below is some code snippet that I have been using:
function traverse(o, d) {
for (i in o) {
if (typeof(o[i])=="object") {
console.log(i, o[i]);
// considering only 'default' obj for now
if(i === 'defaults') {
d.children.push( {obj: i, "children" :[ o[i] ]});
}
traverse(o[i], d);
}
}
return d;
}
However, the current output does not include the necessary arrays within nested objects:
{"children":[{"obj":"defaults",
"children":[{"job":{"example":
{"executors_num":"2","freq_in_mins":"180","executors_mem":"256M","job_name":"batch_example"}}}]}]}
I seem to be stuck at this point and unsure how to incorporate the children
array into every nested object. Any suggestions?