1

Convert following json in tree structure

data = {
  "a": {
    "b": [
      {
        "c": "v1",
        "f": [
          "v3",
          "v4"
        ]
      }
    ]
  }
}

Actually I want to use angular2 tree package to display the above json in tree level so Expected output should be

nodes = [
  {
    "name": "a",
    "children": [
      {
        "name": "b",
        "children": [
          {
            "name": "c"
          },
          {
            "name": "f",
            "children": [
              {
                "name": "v3"
              },
              {
                "name": "v4"
              }
            ]
          }
        ]
      }
    ]
  }
]

I'm trying following but not work to convert above json in tree level

const getFinalNode = function(data, nodes){
       if (typeof data === 'object'){
       Object.keys(data).forEach(function(key){
          let tmpObj = {
            name: key,
            children: []
          }
          if(typeof key === "object"){
            tmpObj.children = getFinalNode(data[key])
          }
          nodes.push(tmpObj)
        })
       }elseif(data instanceof Array){
        arr.forEach(function(obj, idx){
          let tmpObj = {
            name: obj,
            children: []
          }
          if(data instanceof Array){
            tmpObj.children = getFinalNode(obj.children)
          }
          arr[idx] = tmpObj
        })
        return arr
       }
        return nodes
      }
getFinalNode(data, nodes);

Actually I want to display json in following way.

enter image description here

NIKHIL RANE
  • 4,012
  • 2
  • 22
  • 45
  • 2
    That's not [JSON](http://json.org). _"JSON is a textual, language-indepedent data-exchange format, much like XML, CSV or YAML."_ - [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – Andreas Dec 04 '17 at 11:40
  • https://stackoverflow.com/questions/45466419/is-there-a-json-formatter-directive-component-for-angular – yurzui Dec 04 '17 at 12:05

0 Answers0