I have a nested JSON object and I would like to iterate it and build a new object from it but I'm having trouble. I come from a PHP background and this kid of thing is pretty simple with PHP - one just builds an array and doens't have to worry about the element parent not existing as long as the path is there PHP will create it, alas this is not the case with Javascript.
My JSON is the following.
{
"routes": {
"/": {
"label": "Index",
"id": 1
},
"/contact": {},
"/child0": {
"label": "Index",
"id": 1
},
"/child1": {
"label": "Child 1",
"id": 5
},
"/child2": {
"label": "Child 2",
"id": 6
},
"/subchild1": {
"label" : "SUB Child 1",
"id" : 7
}
},
"test": {
"/": {
"children": {
"/child0": {},
"/child1": {},
"/child2": {
"children": {
"/subchild1": {}
}
}
}
}
}
}
What I would like to do is take the "test" part of the object and build a new object with the contents of the output[ROUTE] (i/e: output[/]['children'][{"label":"Child 1","route":"/child1","id".....},{....}]
which would then be turned into a JSON object something like the following.
{
"output": {
"/": {
"label": "Index",
"id": 1,
"route": "/",
"children": [
{
"label": "Child 1",
"id": 5,
"route": "/child1"
},
{
"label": "Child 2",
"id": 6,
"route": "/child2",
"children": [
{
"label": "SUB Child 1",
"id": 7,
"route": "/subchild1"
}
]
}
]
}
}
}
I am using lodash but I am open to any libraries that will make it easier to accomplish.
This has been driving me mad for a week!
Thanks in advance for all help / contributions