I am trying to write a recursive function to parse a JSON object where the structure of my JSON object is like this
const dataToParse = {
key: "someKey",
someArbData: "",
children: [
{
key: "someKey1",
someArbData: "",
children: [
{
key: "someKey5",
....
},
{
key: "someKey6"
}
]
},
{
key: "someKey2",
someArbData: "",
children: [
{
key: "someKey3",
....
},
{
key: "someKey4"
}
]
}
]
}
Basically I have a list where there's nested layers of children as shown above.
My goal is to parse this unreadable JSON object to a map in javascript where it would look like:
const parsedMap = {
"someKey": {
someArbData: "",
children: [
{
key: "someKey1",
someArbData: "",
children: [
{
key: "someKey5",
....
},
{
key: "someKey6"
}
]
},
{
key: "someKey2",
someArbData: "",
children: [
{
key: "someKey3",
....
},
{
key: "someKey4"
}
]
}
]
},
"someKey1": {
someArbData: "",
children: [
{
key: "someKey5",
....
},
{
key: "someKey6"
}
]
},
"someKey2": {
someArbData: "",
children: [
{
key: "someKey3",
....
},
{
key: "someKey4"
}
]
}
}
I was initially going to do a loop but the nesting level cannot be determined ahead of time. So, I was thinking of writing a recursive function in javascript.