Convert flat JSON file to hierarchical json data like flare.json



Answers

var data = [
    { "state": "UP", "district": "Agra", "block": "IradatNagar","school":"APS","name":"A" },
    { "state": "UP", "district": "Agra", "block": "IradatNagar","school":"IPS","name":"B" },
    { "state": "UP", "district": "Agra", "block": "IradatNagar","school":"APS","name":"C" },
    { "state": "MP", "district": "Bhopal", "block": "chota_Bhopal","school":"DPS","name":"D" },
    { "state": "UP", "district": "Mathura", "block": "Farah","school":"HPS","name":"E" },
    { "state": "UP", "district": "Kanpur", "block": "Mania","school":"BPs","name":"F" },
    { "state": "UP", "district": "Agra", "block": "Arjun Nagar","school":"GPS","name":"G" }, 
    { "state": "MP", "district": "Gwalior", "block": "Surya Nagar","school":"DPS","name":"H" }
];

var newData = { name :"State", children : [] },
    levels = ["state","district","block","school"];

data.forEach(function(d){
    var depthCursor = newData.children;
    levels.forEach(function( property, depth )
    {
        var index;
        depthCursor.forEach(function(child,i)
        {
            if ( d[property] == child.name ) 
                index = i;
        });

        if ( isNaN(index) ) 
        {
            depthCursor.push({name : d[property], children : []});
            index = depthCursor.length - 1;
        }

        depthCursor = depthCursor[index].children;

        if ( depth === levels.length - 1 )
        {
            depthCursor.push({ name : d.name});
        }
    });
});

console.log(newData);
Question

After a troublesome fight i almost figured how to convert a flat json file to a Hierarchical one. I didn't write the function by my own. I copied it from the below post.

D3 JSON DATA CONVERSION

But now the problem now is, the function which was written in the post has just 2 levels of hierarchy. But i'm looking for 4 levels hierarchy. I tried to override the function where i failed but.

Code with what i'm trying.

    
         var data = [
        { "dep": "First Top", "name": "First child", "model": "value1", "size": "320" },
        { "dep": "First Top", "name": "First child", "model": "value2", "size": "320" },
        { "dep": "First Top", "name": "First child", "model": "value3", "size": "320" },
        { "dep": "First Top", "name": "First child", "model": "value4", "size": "320" },
        { "dep": "First Top", "name": "SECOND CHILD", "model": "value1", "size": "320" },
        { "dep": "First Top", "name": "SECOND CHILD", "model": "value2", "size": "320" },
        { "dep": "First Top", "name": "SECOND CHILD", "model": "value3", "size": "320" },
        { "dep": "First Top", "name": "SECOND CHILD", "model": "value4", "size": "320" },
        { "dep": "Second Top", "name": "First Child", "model": "value1", "size": "320" },
        { "dep": "Second Top", "name": "First Child", "model": "value2", "size": "320" },
        { "dep": "Second Top", "name": "First Child", "model": "value3", "size": "320" },
        { "dep": "Second Top", "name": "First Child", "model": "value4", "size": "320" },
        { "dep": "Second Top", "name": "SECOND CHILD", "model": "value1", "size": "320" },
        { "dep": "Second Top", "name": "SECOND CHILD", "model": "value2", "size": "320" },
        { "dep": "Second Top", "name": "SECOND CHILD", "model": "value3", "size": "320" },
        { "dep": "Second Top", "name": "SECOND CHILD", "model": "value4", "size": "320" },
        { "dep": "Third Top", "name": "First Child", "model": "value2", "size": "320" },
        { "dep": "Third Top", "name": "First Child", "model": "value3", "size": "320" },
        { "dep": "Third Top", "name": "First Child", "model": "value4", "size": "320" },
        { "dep": "Third Top", "name": "First Child", "model": "value5", "size": "320" },
        { "dep": "Third Top", "name": "Second Child", "model": "value1", "size": "320" },
        { "dep": "Third Top", "name": "Second Child", "model": "value2", "size": "320" },
        { "dep": "Third Top", "name": "Second Child", "model": "value3", "size": "320" },
        { "dep": "Third Top", "name": "Second Child", "model": "value4", "size": "320" }
      ]

    var newData = {"name":"root", "children":{}}

    data.forEach(function(d){
        if(typeof newData.children[d.dep] !== 'undefined')  {
            newData.children[d.dep].children.push(d)
        } else {
            newData.children[d.dep] = {"name": d.dep, "children": [{"name": d.name, "children": [{"name": d.model, "size": d.size}]}]}
        }
    })





    newData.children = Object.keys(newData.children).map(function (key) { return newData.children[key]; });

              // show what we've got
              d3.select('body').append('pre')
                  .text(JSON.stringify(newData, null, '  '));
        

Output of the current code

    {
      "name": "root",
      "children": [
        {
          "name": "First Top",
          "children": [
            {
              "name": "First child",
              "children": [
                {
                  "name": "value1",
                  "size": "320"
                }
              ]
            },
            {
              "dep": "First Top",
              "name": "First child",
              "model": "value2",
              "size": "320"
            },
            {
              "dep": "First Top",
              "name": "First child",
              "model": "value3",
              "size": "320"
            },
            {
              "dep": "First Top",
              "name": "First child",
              "model": "value4",
              "size": "320"
            },
            {
              "dep": "First Top",
              "name": "SECOND CHILD",
              "model": "value1",
              "size": "320"
            },
            {
              "dep": "First Top",
              "name": "SECOND CHILD",
              "model": "value2",
              "size": "320"
            },
            {
              "dep": "First Top",
              "name": "SECOND CHILD",
              "model": "value3",
              "size": "320"
            },
            {
              "dep": "First Top",
              "name": "SECOND CHILD",
              "model": "value4",
              "size": "320"
            }
          ]
        },
        {
          "name": "Second Top",
          "children": [
            {
              "name": "First Child",
              "children": [
                {
                  "name": "value1",
                  "size": "320"
                }
              ]
            },
            {
              "dep": "Second Top",
              "name": "First Child",
              "model": "value2",
              "size": "320"
            },
            {
              "dep": "Second Top",
              "name": "First Child",
              "model": "value3",
              "size": "320"
            },
            {
              "dep": "Second Top",
              "name": "First Child",
              "model": "value4",
              "size": "320"
            },
            {
              "dep": "Second Top",
              "name": "SECOND CHILD",
              "model": "value1",
              "size": "320"
            },
            {
              "dep": "Second Top",
              "name": "SECOND CHILD",
              "model": "value2",
              "size": "320"
            },
            {
              "dep": "Second Top",
              "name": "SECOND CHILD",
              "model": "value3",
              "size": "320"
            },
            {
              "dep": "Second Top",
              "name": "SECOND CHILD",
              "model": "value4",
              "size": "320"
            }
          ]
        },
        {
          "name": "Third Top",
          "children": [
            {
              "name": "First Child",
              "children": [
                {
                  "name": "value2",
                  "size": "320"
                }
              ]
            },
            {
              "dep": "Third Top",
              "name": "First Child",
              "model": "value3",
              "size": "320"
            },
            {
              "dep": "Third Top",
              "name": "First Child",
              "model": "value4",
              "size": "320"
            },
            {
              "dep": "Third Top",
              "name": "First Child",
              "model": "value5",
              "size": "320"
            },
            {
              "dep": "Third Top",
              "name": "Second Child",
              "model": "value1",
              "size": "320"
            },
            {
              "dep": "Third Top",
              "name": "Second Child",
              "model": "value2",
              "size": "320"
            },
            {
              "dep": "Third Top",
              "name": "Second Child",
              "model": "value3",
              "size": "320"
            },
            {
              "dep": "Third Top",
              "name": "Second Child",
              "model": "value4",
              "size": "320"
            }
          ]
        }
      ]
    }

Desired Output Format:

    {
      "name": "root",
      "children": [
        {
          "name": "First Top",
          "children": [
            {
              "name": "First child",
              "children": [
                {
                  "name": "value1",
                  "size": "320"
                },
                {
                  "name": "value2",
                  "size": "320"
                },
                {
                  "name": "value3",
                  "size": "320"
                },
                {
                  "name": "value4",
                  "size": "320"
                }
              ]
            },
            {
              "name": "Second child",
              "children": [
                {
                  "name": "value1",
                  "size": "320"
                },
                {
                  "name": "value2",
                  "size": "320"
                },
                {
                  "name": "value3",
                  "size": "320"
                },
                {
                  "name": "value4",
                  "size": "320"
                }
              ]
            },

          ]
        },
        {
          "name": "Second Top",
          "children": [
            {
              "name": "First child",
              "children": [
                {
                  "name": "value1",
                  "size": "320"
                },
                {
                  "name": "value2",
                  "size": "320"
                },
                {
                  "name": "value3",
                  "size": "320"
                },
                {
                  "name": "value4",
                  "size": "320"
                }
              ]
            },
            {
              "name": "Second child",
              "children": [
                {
                  "name": "value1",
                  "size": "320"
                },
                {
                  "name": "value2",
                  "size": "320"
                },
                {
                  "name": "value3",
                  "size": "320"
                },
                {
                  "name": "value4",
                  "size": "320"
                }
              ]
            },
          ]
        },
        {
          "name": "Third Top",
          "children": [
            {
              "name": "First child",
              "children": [
                {
                  "name": "value1",
                  "size": "320"
                },
                {
                  "name": "value2",
                  "size": "320"
                },
                {
                  "name": "value3",
                  "size": "320"
                },
                {
                  "name": "value4",
                  "size": "320"
                }
              ]
            },
            {
              "name": "Second child",
              "children": [
                {
                  "name": "value1",
                  "size": "320"
                },
                {
                  "name": "value2",
                  "size": "320"
                },
                {
                  "name": "value3",
                  "size": "320"
                },
                {
                  "name": "value4",
                  "size": "320"
                }
              ]
            },
          ]
        }
      ]
    }

I'm cracking my head from a week but i alone couldn't figure it out. Someone please amend the function to get the data in the hierarchical format as i've updated.

Thanks in advance!!




Node.js - Convert flat json to hierarchical json without 'parent','child' attributes

Check out the shape-json module on NPM.

It will make your life much simpler.




another option is to use the nest method built into D3....

var nested = d3.nest()
.key(function(d,i){ return d.dep; })
.entries(data);

which outputs:

 [
  {
    "key": "d1",
    "values": [
      {
        "dep": "d1",
        "name": "name1",
        "size": "size1"
      },
      {
        "dep": "d1",
        "name": "name2",
        "size": "size2"
      }
    ]
  },
  {
    "key": "d2",
    "values": [
      {
        "dep": "d2",
        "name": "name1",
        "size": "size3"
      },
      {
        "dep": "d2",
        "name": "name2",
        "size": "size4"
      }
    ]
  }
]

JsFiddle: http://jsfiddle.net/imrane/bSGrG/1/




Links



Tags