lundi 27 juin 2016

function to read through array of unknown object and print relationship between each of the elements

I am trying to read through an array of nested objects as shown below and create two set of array. one with all the elements,index and the second one with relationship between this elements based on the JSON

My sample JSON is

var data = {
   "device":{
      "name":"Device1",
      "config":{
         "interface":[
            {
               "Loopback":{
                  "name":"{loopback-intf}",
                  "description":{
                     "_tags":"merge",
                     "__text":"Loopback{loopback-intf}"
                  },
                  "ip":{
                     "address":{
                        "primary":{
                           "address":"{ip-address}",
                           "mask":"255.255.255.255"
                        }
                     }
                  }
               },
               "_xmlns":"urn:www",
               "_tags":"merge"
            },
            {
               "Loopback":{
                  "name":"{loopback-intf}",
                  "description":{
                     "_tags":"merge",
                     "__text":"Loopback{loopback-intf}"
                  },
                  "ip":{
                     "address":{
                        "primary":{
                           "address":"{ip-address}",
                           "mask":"255.255.255.255"
                        }
                     }
                  }
               },
               "_xmlns":"http://ww.com/test/xx-xr",
               "_tags":"merge"
            }
         ]
      }
   }
};

The Code I use to create these array is var j=100; function iter(obj,from) { for (var key in obj) { var p=j; if (typeof(obj[key]) == 'object') { if (!Array.isArray(obj)) {

                                  nodeDataArray.push({ key:j,Data: key });
                                  if (from !== undefined) {
                                      if(from == j){
                                          linkDataArray.push({ from: from-1, to: j });
                                      }
                                      else{
                                          linkDataArray.push({ from: from, to: j });
                                      }

                                }
                                  j++;
                                //console.log(key);
                              }
                              //console.log(p,obj[key]);
                              iter(obj[key],p);
                            } else {

                                nodeDataArray.push({ key:j,Data: key + " : " + obj[key]});

                                if (from !== undefined) {
                                    linkDataArray.push({ from: from, to: j });
                                }
                                j++;

                            }
                          }
                        } 

The above code iterates through the provided JSON and pushes data into two arrays

  1. nodeDataArray--> This contains data and key of each element, please see below how NDA will look with first few elements

    nodeDataArray: [{key:100,Data:Device},{key:101,Data:name:device1},{key:102,Data:config},{key:103,Data:Interface},..........until the end of JSON ]

  2. linkDataArray: This has array stores information on who is parent and child based on the Input Json, please see the commented code for more info on what from to value means

    "linkDataArray": [ {"from":100, "to":101},// This means Device is parent of name:Device1 in nodeDataArray which has key 101 {"from":100, "to":102},//This means Device is parent of config, which has key 102 in NodeDataArray(NDA) {"from":102, "to":103},// Config is parent of interface which has key 103 {"from":103, "to":104},//interface is parent of loopback which has key 104 in nodeDataArray {"from":104, "to":105}, {"from":104, "to":106}, {"from":106, "to":107}, {"from":106, "to":108}, {"from":104, "to":109}, {"from":109, "to":110}, {"from":110, "to":111}, {"from":111, "to":112}, {"from":111, "to":113}, {"from":104, "to":114}, {"from":104, "to":115}, {"from":115, "to":116}, {"from":116, "to":117}, {"from":116, "to":118}, {"from":118, "to":119}, {"from":118, "to":120}, {"from":116, "to":121}, {"from":121, "to":122}, {"from":122, "to":123}, {"from":123, "to":124}, {"from":123, "to":125}, {"from":116, "to":126}, {"from":116, "to":127} ]

But i have issue in this for the values

**{"from":115, "to":116}**

But it should have been

{"from":104, "to":116}

based on the input JSON. Can anyone recommend what i am doing wrong

Aucun commentaire:

Enregistrer un commentaire