0

Suppose I have a JSON like the following:

   {
  "filestore": {
    "root": {
      "abc": {
        "def": {
          "wavs": {
            "wav": [
              {
                "id": "1",
                "file": "\\abc\\def\\a.wav",
                "checked": "false"
              },
              {
                "id": "2",
                "file": "\\abc\\def\\b.wav",
                "checked": "false"
              },
              {
                "id": "3",
                "file": "\\abc\\def\\c.wav",
                "checked": "false"
              }
            ]
          }
        },
        "ghi": {
          "wavs": {
            "wav": [
              {
                "id": "1",
                "file": "\\abc\\ghi\\a.wav",
                "checked": "false"
              },
              {
                "id": "2",
                "file": "\\abc\\ghi\\b.wav",
                "checked": "false"
              },
              {
                "id": "3",
                "file": "\\abc\\ghi\\c.wav",
                "checked": "false"
              }
            ]
          }
        },
        "wavs": {
          "wav": [
            {
              "id": "4",
              "file": "\\abc\\x.wav",
              "checked": "false"
            },
            {
              "id": "5",
              "file": "\\abc\\y.wav",
              "checked": "false"
            },
            {
              "id": "6",
              "file": "\\abc\\z.wav",
              "checked": "false"
            }
          ]
        }
      },
      "wavs": {
        "wav": [
          {
            "id": "7",
            "file": "p.wav",
            "checked": "false"
          },
          {
            "id": "8",
            "file": "q.wav",
            "checked": "false"
          },
          {
            "id": "9",
            "file": "r.wav",
            "checked": "false"
          }
        ]
      }
    }
  }
}

Now, I want all the entities within "root". I know that we can create a JSON object within Javascript using JSON.parse and access them individually like filestore.root.abc etc.

But, I want an utility which can extract all the tags within a particular tag. Like, if I send that utility "filestore.root" it should return in an array ["abc", "wavs"]. Similarly, if I send "filestore.root.abc", it should return ["def", "ghi", "wavs"].

Is something like this possible in Javascript ?? Please let me know. I would greatly prefer javascript, but I am open to jquery also. Thanks a lot.

StackAddict
  • 423
  • 9
  • 21

2 Answers2

3

Object.keys(filestore.root); returns ["abc", "wavs"] in modern browsers. Check this relevant SO Answer: https://stackoverflow.com/a/208020/2193252

Community
  • 1
  • 1
darthmaim
  • 4,970
  • 1
  • 27
  • 40
0

If you like DIY i'm using this

var result = [];
for (var key in filestore.root) result.push(key);       
alert(JSON.stringify(result));
imkrisna
  • 853
  • 5
  • 7