Working with JSON without for loop

Hi All,

Here, I would like to give an JSON sample.

Hi,
I would like to share about Object.kyes in JQuery.
I need the output from the below JSON formate with out for or for each loop condition.

var menuJsonObject=
{
  "MenuOne":{  // Level
   "TabsrHeadersItems":{  // Sub Level
     "Tabs":true,  // Inner Level -1
     "TabsItemsValues":[{"content":"New Values1"},{"content":"New Values2"},{"content":"New Values3"}], // Innerr Level -2
      "TabsHeaders":[{"content":"Header 1"}] // Inner Level -3
     }
   }
}
Concept of accessing the JSON Object from the above:
1. JSON object stores value like array (step - by - step process and we cant jump over through one element to another).

   Output: MenuOne -> TabsrHeadersItems -> [1] -> TabsItemsValues -> content -> values

   Output Error: TestLinks -> content => we cant jump over from start to end. we can access the values are one by one through keys in object.

2. we can retrieve the particular objects based on that particular key and that element position using step - by - step process.

 For example: I need first key from main json object. please find the below:

  " Object.keys(menuJsonObject) "

 So the output is " TestLinks,MenuOne " (that is 0,1 from the menuJsonObject).

 If we need the objects one by one then we can use the below format:
 " Object.keys(menuJsonObject)[0]  or Object.keys(menuJsonObject)[1] "

3. If we need the inner level element then we can use the following format:

  Example: we need the content value means we need to access the "content" object then it is return values automatically.

 The static content value's formate:

 menuJSonObject["MenuOne"]["TabsrHeadersItems"][1]["TabsItemsValues"]["content"][0]
// After that we can loop through the values

 The Dymanic Accessing:

  Step 1: Object.keys(menuJsonObject) ===> "MenuOne"

  Step 2: Object.keys(menuJsonObject[Object.keys(menuJsonObject)])  ===> "TabsrHeadersItems"

  Here we need to note down one thing. that is, whenever we use the only one "Object.keys" then it is returned the first key from menuJsonObject.

  Here we used two "Object.keys". So it is returned the sub level value from the menuJsonObject.

  Because the first "Object.key" refers the immediate key. 

  This process is continued up to the last element value.

  Step 3: Object.keys(menuJsonObject [Object.keys(menuJsonObject)] [Object.keys(menuJsonObject[Object.keys(menuJsonObject)])] )  ===> "Tabs, TabsItemsValues"

  Step 4: Object.keys(menuJsonObject [Object.keys(menuJsonObject)] [Object.keys(menuJsonObject[Object.keys(menuJsonObject)])] )[1]  ===> "TabsItemsValues"

  Step 5: Object.keys(menuJsonObject[Object.keys(menuJsonObject)][Object.keys(menuJsonObject[Object.keys(menuJsonObject)])]
                      [Object.keys(menuJsonObject[Object.keys(menuJsonObject)][Object.keys(menuJsonObject[Object.keys(menuJsonObject)])] )[1]]
        [0])  ===> "content"

  Step 6: The Final step for dynamic output is

var objLen=
(menuJsonObject
[Object.keys(menuJsonObject)]
[Object.keys(menuJsonObject[Object.keys(menuJsonObject)])]
[Object.keys(menuJsonObject
[Object.keys(menuJsonObject)]
[Object.keys(menuJsonObject[Object.keys(menuJsonObject)])]
)[1]
]).length
;

for(var i=0;i<objLen;i++)
{
  menuJsonObject
[Object.keys(menuJsonObject)]
[Object.keys(menuJsonObject[Object.keys(menuJsonObject)])]
[Object.keys(menuJsonObject[Object.keys(menuJsonObject)][Object.keys(menuJsonObject[Object.keys(menuJsonObject)])] )[1]]
[i] //ToDO-For loop then only we get all values
//["content"]
//To get "content" then we need object.keys to all of the above (whole)
[Object.keys(
menuJsonObject
[Object.keys(menuJsonObject)]
[Object.keys(menuJsonObject[Object.keys(menuJsonObject)])]
[Object.keys(menuJsonObject[Object.keys(menuJsonObject)][Object.keys(menuJsonObject[Object.keys(menuJsonObject)])] )[1]]
[0]
)]
}

Here we get all content values from "TabsItemsValues" key.
Thanks...

Comments