How to get nth level of parsed json?












0















I have a JSON that has three levels and it's stored as Array I need to get 2nd level(count started from 0) and below in each level has 10+ elements. How it can be implemented on JavaScript.Need your help. All response will be helpful.



ps. some 1st level and 2nd level elements can be empty



[
{
"name": "0th level first", //0th level
"options": [
{
"name": "1st level Cafe", // 1st level
"options": [
{
"name": "2nd level Staff", //2nd level
"options": [
{
"name": "Gary", //3rd level
"age": 18
},
{
"name": "James", //3rd level
"age": 25
}
]
}
]
}
]
}
]









share|improve this question

























  • json you provide is not valid

    – Kamil Kiełczewski
    Nov 22 '18 at 14:34











  • @KamilKiełczewski what's wrong with it?

    – charlietfl
    Nov 22 '18 at 14:36











  • provide how the output json (result you wan't to get) should look like

    – Kamil Kiełczewski
    Nov 22 '18 at 14:37








  • 1





    Possible duplicate of Access / process (nested) objects, arrays or JSON

    – str
    Nov 22 '18 at 14:40






  • 1





    I just came up with a solution, that I wouldn't consider the best in terms of readability, and also support (it's experimental) but I just wanted to try out the flatMap function, which isn't a part of Node.JS for instance, so be careful of it's support across browsers: arr.flatMap(firstLevel => firstLevel.options.flatMap(secondLevel => secondLevel.options.flatMap(finalLevel => finalLevel.options)));

    – abdullahkady
    Nov 22 '18 at 14:42


















0















I have a JSON that has three levels and it's stored as Array I need to get 2nd level(count started from 0) and below in each level has 10+ elements. How it can be implemented on JavaScript.Need your help. All response will be helpful.



ps. some 1st level and 2nd level elements can be empty



[
{
"name": "0th level first", //0th level
"options": [
{
"name": "1st level Cafe", // 1st level
"options": [
{
"name": "2nd level Staff", //2nd level
"options": [
{
"name": "Gary", //3rd level
"age": 18
},
{
"name": "James", //3rd level
"age": 25
}
]
}
]
}
]
}
]









share|improve this question

























  • json you provide is not valid

    – Kamil Kiełczewski
    Nov 22 '18 at 14:34











  • @KamilKiełczewski what's wrong with it?

    – charlietfl
    Nov 22 '18 at 14:36











  • provide how the output json (result you wan't to get) should look like

    – Kamil Kiełczewski
    Nov 22 '18 at 14:37








  • 1





    Possible duplicate of Access / process (nested) objects, arrays or JSON

    – str
    Nov 22 '18 at 14:40






  • 1





    I just came up with a solution, that I wouldn't consider the best in terms of readability, and also support (it's experimental) but I just wanted to try out the flatMap function, which isn't a part of Node.JS for instance, so be careful of it's support across browsers: arr.flatMap(firstLevel => firstLevel.options.flatMap(secondLevel => secondLevel.options.flatMap(finalLevel => finalLevel.options)));

    – abdullahkady
    Nov 22 '18 at 14:42
















0












0








0








I have a JSON that has three levels and it's stored as Array I need to get 2nd level(count started from 0) and below in each level has 10+ elements. How it can be implemented on JavaScript.Need your help. All response will be helpful.



ps. some 1st level and 2nd level elements can be empty



[
{
"name": "0th level first", //0th level
"options": [
{
"name": "1st level Cafe", // 1st level
"options": [
{
"name": "2nd level Staff", //2nd level
"options": [
{
"name": "Gary", //3rd level
"age": 18
},
{
"name": "James", //3rd level
"age": 25
}
]
}
]
}
]
}
]









share|improve this question
















I have a JSON that has three levels and it's stored as Array I need to get 2nd level(count started from 0) and below in each level has 10+ elements. How it can be implemented on JavaScript.Need your help. All response will be helpful.



ps. some 1st level and 2nd level elements can be empty



[
{
"name": "0th level first", //0th level
"options": [
{
"name": "1st level Cafe", // 1st level
"options": [
{
"name": "2nd level Staff", //2nd level
"options": [
{
"name": "Gary", //3rd level
"age": 18
},
{
"name": "James", //3rd level
"age": 25
}
]
}
]
}
]
}
]






javascript json parsing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 16:14







Yerkebulan Doroshev

















asked Nov 22 '18 at 14:33









Yerkebulan DoroshevYerkebulan Doroshev

13




13













  • json you provide is not valid

    – Kamil Kiełczewski
    Nov 22 '18 at 14:34











  • @KamilKiełczewski what's wrong with it?

    – charlietfl
    Nov 22 '18 at 14:36











  • provide how the output json (result you wan't to get) should look like

    – Kamil Kiełczewski
    Nov 22 '18 at 14:37








  • 1





    Possible duplicate of Access / process (nested) objects, arrays or JSON

    – str
    Nov 22 '18 at 14:40






  • 1





    I just came up with a solution, that I wouldn't consider the best in terms of readability, and also support (it's experimental) but I just wanted to try out the flatMap function, which isn't a part of Node.JS for instance, so be careful of it's support across browsers: arr.flatMap(firstLevel => firstLevel.options.flatMap(secondLevel => secondLevel.options.flatMap(finalLevel => finalLevel.options)));

    – abdullahkady
    Nov 22 '18 at 14:42





















  • json you provide is not valid

    – Kamil Kiełczewski
    Nov 22 '18 at 14:34











  • @KamilKiełczewski what's wrong with it?

    – charlietfl
    Nov 22 '18 at 14:36











  • provide how the output json (result you wan't to get) should look like

    – Kamil Kiełczewski
    Nov 22 '18 at 14:37








  • 1





    Possible duplicate of Access / process (nested) objects, arrays or JSON

    – str
    Nov 22 '18 at 14:40






  • 1





    I just came up with a solution, that I wouldn't consider the best in terms of readability, and also support (it's experimental) but I just wanted to try out the flatMap function, which isn't a part of Node.JS for instance, so be careful of it's support across browsers: arr.flatMap(firstLevel => firstLevel.options.flatMap(secondLevel => secondLevel.options.flatMap(finalLevel => finalLevel.options)));

    – abdullahkady
    Nov 22 '18 at 14:42



















json you provide is not valid

– Kamil Kiełczewski
Nov 22 '18 at 14:34





json you provide is not valid

– Kamil Kiełczewski
Nov 22 '18 at 14:34













@KamilKiełczewski what's wrong with it?

– charlietfl
Nov 22 '18 at 14:36





@KamilKiełczewski what's wrong with it?

– charlietfl
Nov 22 '18 at 14:36













provide how the output json (result you wan't to get) should look like

– Kamil Kiełczewski
Nov 22 '18 at 14:37







provide how the output json (result you wan't to get) should look like

– Kamil Kiełczewski
Nov 22 '18 at 14:37






1




1





Possible duplicate of Access / process (nested) objects, arrays or JSON

– str
Nov 22 '18 at 14:40





Possible duplicate of Access / process (nested) objects, arrays or JSON

– str
Nov 22 '18 at 14:40




1




1





I just came up with a solution, that I wouldn't consider the best in terms of readability, and also support (it's experimental) but I just wanted to try out the flatMap function, which isn't a part of Node.JS for instance, so be careful of it's support across browsers: arr.flatMap(firstLevel => firstLevel.options.flatMap(secondLevel => secondLevel.options.flatMap(finalLevel => finalLevel.options)));

– abdullahkady
Nov 22 '18 at 14:42







I just came up with a solution, that I wouldn't consider the best in terms of readability, and also support (it's experimental) but I just wanted to try out the flatMap function, which isn't a part of Node.JS for instance, so be careful of it's support across browsers: arr.flatMap(firstLevel => firstLevel.options.flatMap(secondLevel => secondLevel.options.flatMap(finalLevel => finalLevel.options)));

– abdullahkady
Nov 22 '18 at 14:42














3 Answers
3






active

oldest

votes


















0














Probably this one? :)



  data.forEach((value, index) => {
for(stage0 in value){
if(typeof value[stage0] === 'object'){
value[stage0].forEach((val, index) => {
for(stage1 in val){
if(typeof val[stage1] === 'object'){
val[stage1].forEach((val2, index) => {
for(stage2 in val2){
console.log(val2[stage2]);
}
})
}
}
})
}
}
})





share|improve this answer































    0














    You can use array.forEach which will be only iterating through each level.



    on first loop there is a property call options which is an array, you need to loop through the options array in 0 level, on the second loop again one more options array comes you need to again loop through the options array which is 1 level.
    then you reach the thrid level which is your output.



    Third level means starting from zero its second level.



    I hope this will solve the issue.






    var data = [
    {
    "name": "0th level first", //0th level
    "options": [
    {
    "name": "1st level Cafe", // 1st level
    "options": [
    {
    "name": "2nd level Staff", //2nd level
    "options": [
    {
    "name": "Gary", //3rd level
    "age": 18
    },
    {
    "name": "James", //3rd level
    "age": 25
    }
    ]
    }
    ]
    }
    ]
    }
    ]

    data.forEach(fl => {
    fl.options.forEach(sl => {
    sl.options.forEach(tl => {
    console.log("second level starting from 0",tl)
    })
    })
    })








    share|improve this answer































      0














      Assume data contains your json, we have 2 onliners:



      let out = data[0].options[0].options[0];

      let outArr = data.flatMap(x=>x.options.flatMap(y=>y.options));


      results



      // out = {"name":"2nd level Staff","options":[{"name":"Gary","age":18},{"name":"James","age":25}]}

      // outArr = [{"name":"2nd level Staff","options":[{"name":"Gary","age":18},{"name":"James","age":25}]}]


      First one (out) contains one 2nd element. We use here tree indexes here (three zeros) first data[0] return first element in array data, then options[0] return first element in options array.



      The second solution (outArr) contains array of all 2nd elements. We use here JS flatMap



      To write second solution inspired me abdullahkady comment below question.






      share|improve this answer

























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433194%2fhow-to-get-nth-level-of-parsed-json%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        Probably this one? :)



          data.forEach((value, index) => {
        for(stage0 in value){
        if(typeof value[stage0] === 'object'){
        value[stage0].forEach((val, index) => {
        for(stage1 in val){
        if(typeof val[stage1] === 'object'){
        val[stage1].forEach((val2, index) => {
        for(stage2 in val2){
        console.log(val2[stage2]);
        }
        })
        }
        }
        })
        }
        }
        })





        share|improve this answer




























          0














          Probably this one? :)



            data.forEach((value, index) => {
          for(stage0 in value){
          if(typeof value[stage0] === 'object'){
          value[stage0].forEach((val, index) => {
          for(stage1 in val){
          if(typeof val[stage1] === 'object'){
          val[stage1].forEach((val2, index) => {
          for(stage2 in val2){
          console.log(val2[stage2]);
          }
          })
          }
          }
          })
          }
          }
          })





          share|improve this answer


























            0












            0








            0







            Probably this one? :)



              data.forEach((value, index) => {
            for(stage0 in value){
            if(typeof value[stage0] === 'object'){
            value[stage0].forEach((val, index) => {
            for(stage1 in val){
            if(typeof val[stage1] === 'object'){
            val[stage1].forEach((val2, index) => {
            for(stage2 in val2){
            console.log(val2[stage2]);
            }
            })
            }
            }
            })
            }
            }
            })





            share|improve this answer













            Probably this one? :)



              data.forEach((value, index) => {
            for(stage0 in value){
            if(typeof value[stage0] === 'object'){
            value[stage0].forEach((val, index) => {
            for(stage1 in val){
            if(typeof val[stage1] === 'object'){
            val[stage1].forEach((val2, index) => {
            for(stage2 in val2){
            console.log(val2[stage2]);
            }
            })
            }
            }
            })
            }
            }
            })






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 '18 at 15:04









            shakogeleshakogele

            353212




            353212

























                0














                You can use array.forEach which will be only iterating through each level.



                on first loop there is a property call options which is an array, you need to loop through the options array in 0 level, on the second loop again one more options array comes you need to again loop through the options array which is 1 level.
                then you reach the thrid level which is your output.



                Third level means starting from zero its second level.



                I hope this will solve the issue.






                var data = [
                {
                "name": "0th level first", //0th level
                "options": [
                {
                "name": "1st level Cafe", // 1st level
                "options": [
                {
                "name": "2nd level Staff", //2nd level
                "options": [
                {
                "name": "Gary", //3rd level
                "age": 18
                },
                {
                "name": "James", //3rd level
                "age": 25
                }
                ]
                }
                ]
                }
                ]
                }
                ]

                data.forEach(fl => {
                fl.options.forEach(sl => {
                sl.options.forEach(tl => {
                console.log("second level starting from 0",tl)
                })
                })
                })








                share|improve this answer




























                  0














                  You can use array.forEach which will be only iterating through each level.



                  on first loop there is a property call options which is an array, you need to loop through the options array in 0 level, on the second loop again one more options array comes you need to again loop through the options array which is 1 level.
                  then you reach the thrid level which is your output.



                  Third level means starting from zero its second level.



                  I hope this will solve the issue.






                  var data = [
                  {
                  "name": "0th level first", //0th level
                  "options": [
                  {
                  "name": "1st level Cafe", // 1st level
                  "options": [
                  {
                  "name": "2nd level Staff", //2nd level
                  "options": [
                  {
                  "name": "Gary", //3rd level
                  "age": 18
                  },
                  {
                  "name": "James", //3rd level
                  "age": 25
                  }
                  ]
                  }
                  ]
                  }
                  ]
                  }
                  ]

                  data.forEach(fl => {
                  fl.options.forEach(sl => {
                  sl.options.forEach(tl => {
                  console.log("second level starting from 0",tl)
                  })
                  })
                  })








                  share|improve this answer


























                    0












                    0








                    0







                    You can use array.forEach which will be only iterating through each level.



                    on first loop there is a property call options which is an array, you need to loop through the options array in 0 level, on the second loop again one more options array comes you need to again loop through the options array which is 1 level.
                    then you reach the thrid level which is your output.



                    Third level means starting from zero its second level.



                    I hope this will solve the issue.






                    var data = [
                    {
                    "name": "0th level first", //0th level
                    "options": [
                    {
                    "name": "1st level Cafe", // 1st level
                    "options": [
                    {
                    "name": "2nd level Staff", //2nd level
                    "options": [
                    {
                    "name": "Gary", //3rd level
                    "age": 18
                    },
                    {
                    "name": "James", //3rd level
                    "age": 25
                    }
                    ]
                    }
                    ]
                    }
                    ]
                    }
                    ]

                    data.forEach(fl => {
                    fl.options.forEach(sl => {
                    sl.options.forEach(tl => {
                    console.log("second level starting from 0",tl)
                    })
                    })
                    })








                    share|improve this answer













                    You can use array.forEach which will be only iterating through each level.



                    on first loop there is a property call options which is an array, you need to loop through the options array in 0 level, on the second loop again one more options array comes you need to again loop through the options array which is 1 level.
                    then you reach the thrid level which is your output.



                    Third level means starting from zero its second level.



                    I hope this will solve the issue.






                    var data = [
                    {
                    "name": "0th level first", //0th level
                    "options": [
                    {
                    "name": "1st level Cafe", // 1st level
                    "options": [
                    {
                    "name": "2nd level Staff", //2nd level
                    "options": [
                    {
                    "name": "Gary", //3rd level
                    "age": 18
                    },
                    {
                    "name": "James", //3rd level
                    "age": 25
                    }
                    ]
                    }
                    ]
                    }
                    ]
                    }
                    ]

                    data.forEach(fl => {
                    fl.options.forEach(sl => {
                    sl.options.forEach(tl => {
                    console.log("second level starting from 0",tl)
                    })
                    })
                    })








                    var data = [
                    {
                    "name": "0th level first", //0th level
                    "options": [
                    {
                    "name": "1st level Cafe", // 1st level
                    "options": [
                    {
                    "name": "2nd level Staff", //2nd level
                    "options": [
                    {
                    "name": "Gary", //3rd level
                    "age": 18
                    },
                    {
                    "name": "James", //3rd level
                    "age": 25
                    }
                    ]
                    }
                    ]
                    }
                    ]
                    }
                    ]

                    data.forEach(fl => {
                    fl.options.forEach(sl => {
                    sl.options.forEach(tl => {
                    console.log("second level starting from 0",tl)
                    })
                    })
                    })





                    var data = [
                    {
                    "name": "0th level first", //0th level
                    "options": [
                    {
                    "name": "1st level Cafe", // 1st level
                    "options": [
                    {
                    "name": "2nd level Staff", //2nd level
                    "options": [
                    {
                    "name": "Gary", //3rd level
                    "age": 18
                    },
                    {
                    "name": "James", //3rd level
                    "age": 25
                    }
                    ]
                    }
                    ]
                    }
                    ]
                    }
                    ]

                    data.forEach(fl => {
                    fl.options.forEach(sl => {
                    sl.options.forEach(tl => {
                    console.log("second level starting from 0",tl)
                    })
                    })
                    })






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 22 '18 at 15:05









                    DILEEP THOMASDILEEP THOMAS

                    983514




                    983514























                        0














                        Assume data contains your json, we have 2 onliners:



                        let out = data[0].options[0].options[0];

                        let outArr = data.flatMap(x=>x.options.flatMap(y=>y.options));


                        results



                        // out = {"name":"2nd level Staff","options":[{"name":"Gary","age":18},{"name":"James","age":25}]}

                        // outArr = [{"name":"2nd level Staff","options":[{"name":"Gary","age":18},{"name":"James","age":25}]}]


                        First one (out) contains one 2nd element. We use here tree indexes here (three zeros) first data[0] return first element in array data, then options[0] return first element in options array.



                        The second solution (outArr) contains array of all 2nd elements. We use here JS flatMap



                        To write second solution inspired me abdullahkady comment below question.






                        share|improve this answer






























                          0














                          Assume data contains your json, we have 2 onliners:



                          let out = data[0].options[0].options[0];

                          let outArr = data.flatMap(x=>x.options.flatMap(y=>y.options));


                          results



                          // out = {"name":"2nd level Staff","options":[{"name":"Gary","age":18},{"name":"James","age":25}]}

                          // outArr = [{"name":"2nd level Staff","options":[{"name":"Gary","age":18},{"name":"James","age":25}]}]


                          First one (out) contains one 2nd element. We use here tree indexes here (three zeros) first data[0] return first element in array data, then options[0] return first element in options array.



                          The second solution (outArr) contains array of all 2nd elements. We use here JS flatMap



                          To write second solution inspired me abdullahkady comment below question.






                          share|improve this answer




























                            0












                            0








                            0







                            Assume data contains your json, we have 2 onliners:



                            let out = data[0].options[0].options[0];

                            let outArr = data.flatMap(x=>x.options.flatMap(y=>y.options));


                            results



                            // out = {"name":"2nd level Staff","options":[{"name":"Gary","age":18},{"name":"James","age":25}]}

                            // outArr = [{"name":"2nd level Staff","options":[{"name":"Gary","age":18},{"name":"James","age":25}]}]


                            First one (out) contains one 2nd element. We use here tree indexes here (three zeros) first data[0] return first element in array data, then options[0] return first element in options array.



                            The second solution (outArr) contains array of all 2nd elements. We use here JS flatMap



                            To write second solution inspired me abdullahkady comment below question.






                            share|improve this answer















                            Assume data contains your json, we have 2 onliners:



                            let out = data[0].options[0].options[0];

                            let outArr = data.flatMap(x=>x.options.flatMap(y=>y.options));


                            results



                            // out = {"name":"2nd level Staff","options":[{"name":"Gary","age":18},{"name":"James","age":25}]}

                            // outArr = [{"name":"2nd level Staff","options":[{"name":"Gary","age":18},{"name":"James","age":25}]}]


                            First one (out) contains one 2nd element. We use here tree indexes here (three zeros) first data[0] return first element in array data, then options[0] return first element in options array.



                            The second solution (outArr) contains array of all 2nd elements. We use here JS flatMap



                            To write second solution inspired me abdullahkady comment below question.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 22 '18 at 15:40

























                            answered Nov 22 '18 at 14:52









                            Kamil KiełczewskiKamil Kiełczewski

                            10.5k86293




                            10.5k86293






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433194%2fhow-to-get-nth-level-of-parsed-json%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Costa Masnaga

                                Fotorealismo

                                Sidney Franklin