Mike Rifgin
Mike Rifgin

Reputation: 10761

How do i restructure this JSON to be more accessible

I have some JSON data formatted like so:

 [ {"stage1" : [{
        "checkpoints" : 
            [
                { 
                    "id" : "checkpoint1",
                    "name" : "a checkpoint name 1",
                    "purpose" : "a string about the purpose here", 
                                         },
                 { 
                    "id" : "checkpoint2",
                    "name" : "a checkpoint name 2",
                    "purpose" : "a string about the purpose here", 

                 }
             ],                      

          "stages" : 
                  [
                     {

                     "id" : "an id here",
                     "name" : "a checkpoint name 1",
                     "purpose" : "a string about the purpose here yah", 

                     } 
             ]          
         }
     ]},

   {"stage2" : [{
        "checkpoints" : 
            [
                { 
                    "id" : "checkpoint1",
                    "name" : "a checkpoint name 1",
                    "purpose" : "a string about the purpose here", 
                                         },
                 { 
                    "id" : "checkpoint2",
                    "name" : "a checkpoint name 2",
                    "purpose" : "a string about the purpose here", 

                 }
             ],                      

          "stages" : 
                  [
                     {

                     "id" : "an id here",
                     "name" : "a checkpoint name 1",
                     "purpose" : "a string about the purpose here yah", 

                     } 
             ]          
         }
     ]},

     {"stage3" : [{
        "checkpoints" : 
            [
                { 
                    "id" : "checkpoint1",
                    "name" : "a checkpoint name 1",
                    "purpose" : "a string about the purpose here", 
                                         },
                 { 
                    "id" : "checkpoint2",
                    "name" : "a checkpoint name 2",
                    "purpose" : "a string about the purpose here", 

                 }
             ],                      

          "stages" : 
                  [
                     {

                     "id" : "an id here",
                     "name" : "a checkpoint name 1",
                     "purpose" : "a string about the purpose here yah", 

                     } 
             ]          
         }
     ]},





                         ]

Currently I have to reference the data like this to get at a stage:

alert(data[0].stage1[0].checkpoints.length);  

and to get at the stage 2 data I have to do this:

alert(data[1].stage2[0].checkpoints.length); 

What I want to be able to do is just use the stage name to access data without having to specify the index after the 'data' declaration:

alert(data.stagex[0].checkpoints.length); 

I don't want to have to state the index after the data part. How can I restructure my JSON so I can use the stage name to get the data I need without specifying the index first?

Upvotes: 0

Views: 164

Answers (1)

Nikhil Baliga
Nikhil Baliga

Reputation: 1341

Don't put Stage1, Stage2 in an array. They can be directly accessed as keys

Upvotes: 3

Related Questions