Hey there! any question in your mind? Ask It Now!.

Popular Categories

how to get all child nodes in JSON

+4 votes
asked in Programming by sunil

I have a JSON like below :


var source=[{'k':'01'} ,
{'k':'02','children':[{'k':'05'},{'k':'06', 'children':[{'k':'ABC'},{'K':'PQR'}]},{'k':'07'}]},

Here I want to get child like k=02 which would return its all children or grand children if available. Any good solution to get children and grandchildren of that node???

5 Answers

0 votes
answered by anonymous
You can loop through the value that you get when running command 'source[1].children'. You will get all the children object. Which can be further looped to get grandchildren object
0 votes
answered by radhu
if you can use jQuery there is the $.parseJSON() function that will transform your json into an object which you can process easier.
0 votes
answered by Vipin
It seems you are want to read hierarchical data from a JSON string. First you need to parse the data into a JSON object. Then if the number of nesting level is fixed then you should go for iterative method of fetching the data for each child. If the level is unknown then it is the case of recursive function call. Iteration method is always TIME and SPACE effective way of parsing the fixed length levels. But recursive one is a flexible and good for traversing unknown level of childs.
0 votes
answered by Khaled
I hope it'll help you
test_1.php ---------------------------
$data = array(
    'k' => '01',
    'k' => '01',
    'child' => array(),

for ($index = 0; $index < 20; $index++) {
    $data['child'][] = "ch - $index";

echo json_encode($data);

test.php -------------------------------
<html lang="en">
        <meta charset="utf-8">
        <script src="resources/js/jquery-1.8.1.min.js"></script>
            $(function() {
                    url: 'test_1.php',
                    type: 'get',
                    success: function(data){
                        //                        ////alert(data);
                        obj = JSON.parse(data);
                        for(i = 0; i < obj.child.length; i++) {

                    error: function(){
                return false;
        <div id="main" >



to try this script (php) deploy it in your php server then >> http://localhost/test.php

good luck
+1 vote
answered by shadywallas (160 points)

i know it's too late but it was very easy Using recursive Function as you object seems to be the same on all other levels 
Just reformatted the Object for clearer view 
var source = [
        "k": "01"
        "k": "02",
        "children": [
                "k": "05"
                "k": "06",
                "children": [
                        "k": "ABC"
                        "K": "PQR"
                "k": "07"
        "k": "03"
function getChilds(source){ 



          if( x.children != undefined){ 






and Finlay call getChilds(source); it will recursively Traverse the object and print the value of any node it visits 
it's like traversing tree , reading about tree data structure will definitely help you understand better 

commented by Bhartesh (2,690 points)
you can easily use PHP function json_decode and get an array as result

Related Questions

Not a Member yet?

Ask to Folks Login

My Account
132 Folks are online
0 members and 132 guest online
Your feedback is highly appreciated