0

I have a JSON object like the one given below where there the last child element is an array of strings. What I need is that I should pass the "ParentKey" to a function as an input and it should return me all the array items (under each key) pushed into an unified array without having to specify each parent key.

The final output should from the below given object should be like: ["Key1-Key1.1-Val1","Key1-Key1.1-Val2", "Key1-Key1.1-Val3", "Key1-Key1.1-Val4", "Key1-Key1.2-Val1",...]

"Parentkey": {
    "key1": {
        "key1.1": [
            "Val 1",
            "Val 2",
            "Val 3",
            "Val 4"
        ],
        "Key1.2": [
            "Val 1",
            "Val 2",
            "Val 3",
            "Val 4"
        ],
        "Key1.3": [
            "Val 1",
            "Val 2",
            "Val 3",
            "Val 4"
        ]
    },
    "key2": {
        "key2.1": [
            "Val 1",
            "Val 2",
            "Val 3",
            "Val 4"
        ]...
    }
}

The function needs to be such that I must not require to give the names of the child keys but only the "ParentKey" as an object.

Based on this out put, I need to create an angular based directive to show hide elements based on user's access rights.

Thanks.

Nitin Suri
  • 960
  • 1
  • 8
  • 20
  • What have you tried so far? Generally StackOverflow does not do your work for you. You need to tell us what you have tried so far. See [this checklist](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist) and [this page](http://stackoverflow.com/help/on-topic) for more details. – GregL Apr 11 '14 at 03:08
  • 1
    @GregL thanks for your input, I am not expecting anyone to do my work, but only help me. I am a novice with JSON objects and as per my understanding there is no way we can read the value of a key with-out we traverse through the JSON till the last child key. Is my understanding right? If not, would request help doing the same as described in the question above. – Nitin Suri Apr 11 '14 at 03:33

1 Answers1

2

This is essentially a traversal problem. I suggest taking a look at:

Traverse all the Nodes of a JSON Object Tree with JavaScript

as a start to process your JSON object. You can create your unified array as you walk your object.

Community
  • 1
  • 1
wongcode
  • 123
  • 6