1

I want to search in json data with multiple levels of array. My search list return names of my objects but just from the first level. How could i do return all my object's names regardless their levels ? In this example : OST, OST details, Apocalpse Now, Arizona Dream, Dexter

Data

<script type="application/json" id="dataMusic">
{
"name":"Music",
"level":"1",
"size":36184,
"children":[
    {
        "name":"OST",
        "level":"2",
        "size":1416,
        "children":[
            {
                "name":"OST details",
                "level":"3",
                "size":1416,
                "children":[
                    {
                        "name":"Apocalypse Now",
                        "size":15
                    },
                    {
                        "name":"Arizona Dream",
                        "size":19
                    },
                    {
                        "name":"Dexter",
                        "size":20
                    }
                ]
             }
          ]
       }
    ]
 }
 </script>

Function

var dataMusic = document.getElementById('dataMusic').innerHTML; 
var dataTree = JSON.parse(dataMusic);   

var optArray = [];
for (var i = 0; i < dataTree.children.length - 1; i++) {
    optArray.push(dataTree.children[i].name);
    }
    optArray = optArray.sort();

I try this method Parsing Nested Objects in a Json using JS without success

Function

var optArray = [], Music, OST, OST details;
for (Music in dataTree) {
    for (OST in dataTree[Music]) {
        for (OST details in dataTree[Music][OST]) {
            if (OST details in optArray) {
                optArray[OST details].push(dataTree[Music][OST][OST details].name)
            } else {
                    optArray[OST details] = [dataTree[Music][OST][OST details].name] 
            }
        }
    }
}
Community
  • 1
  • 1
GeoGyro
  • 487
  • 12
  • 32

2 Answers2

0

You must use nested loops

for Music.children.length
    for OST.children.length
        for OST details.children.length

Edit : Function

var optArray = [], Music, OST, OST_details;
for (Music in dataTree) {
    for (OST in dataTree[Music]) {
        for (OST_details in dataTree[Music][OST]) {
            if (OST_details in optArray) {
                optArray[OST_details].push(dataTree[Music][OST][OST_details].name)
            } else {
                    optArray[OST_details] = [dataTree[Music][OST][OST_details].name] 
            }
        }
    }
}
ilhan kaya
  • 51
  • 10
  • Thanks for your answer. I try this method [http://stackoverflow.com/questions/15065920/parsing-nested-objects-in-a-json-using-js] without success (For syntax expects `;` see my update above). How do you write it ? – GeoGyro Nov 25 '15 at 15:19
  • I try write function for you dont use any editor, please check it. – ilhan kaya Nov 25 '15 at 15:55
  • Firebug don't return me any bug (syntax is ok) but no object is returned. – GeoGyro Nov 25 '15 at 16:07
  • Check : [**link1**](http://jsfiddle.net/fkling/c5K3r/) and [**link2**](http://stackoverflow.com/questions/4992383/use-jquerys-find-on-json-object) – ilhan kaya Nov 26 '15 at 08:50
  • It works partially, Firebug return me an error `TypeError: cyclic object value alert(JSON.stringify(toArray(dataTree)));` In the meantime i found this method [http://stackoverflow.com/questions/5189387/how-do-i-loop-through-deeply-nested-properties-of-a-json-object] wich seems to work. – GeoGyro Nov 26 '15 at 11:10
0

I got it

var dataMusic = document.getElementById('dataMusic').innerHTML; 
var dataTree = JSON.parse(dataMusic);  
var result = [];   

function getAll( input, target ) {     
    function parseData( input, target ) {
        $.each( input, function ( index, obj ) {
            if ( index == target ) {
                result.push( obj );
            }
            else {
                switch ( $.type( obj ).toLowerCase() ) {
                    case "object":
                    case "array":
                        parseData( obj, target );
                        break;
                }
            }
        });
    }    
    parseData( dataTree, "name" );
    result = result.sort();
    return result;
}
alert(JSON.stringify( getAll( dataTree, "name" )));

Thanks to this post : Parsing multi-level json ; Demo

Community
  • 1
  • 1
GeoGyro
  • 487
  • 12
  • 32