0

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

James Kipling
  • 121
  • 10
  • can't you have php output it for you in the requested format? and javascript works the same as php $obj['foobar'] allows you to use associative keys in objects. `for(key in jsonobject)if(jsonobject.hasOwnProperty(key){ dosomething }` – Tschallacka Sep 12 '16 at 14:31
  • You said, you are using lodash and that you are having trouble. Can you share that part as well. We can help you understand what went wrong. – Rajesh Sep 12 '16 at 14:32
  • Hi no unortunatley I am writing a Javascript application so having PHP output it is not possible. Regarding lodash, I have got as far as a forIn loop but get stuck after that! – James Kipling Sep 12 '16 at 14:34
  • Why are you calling this "JSON", which is a string-based format for exchanging information, usually with eg the server? –  Sep 12 '16 at 16:41
  • Are you serious? Do you even know what JSON is? Why are you arguing semantics (you clearly know nothing about) instead of being helpful or not speaking at all? – James Kipling Sep 29 '16 at 07:39

0 Answers0