-3

I want convert flat-Json to hierarchical, or I'm converting excel to Json first and then try to make JSON Object hierarchy. I want convert flat-Json to hierarchical json object of nth level.

Is there any middleware  that conver Flat JSON to hirerachy JSON object.
{ "Name": "SAM", "Department": "CO", "Year": "2018"},
{ "Name": "SAM", "Address": "Mumbai", "Type": "Permanent"},
{ "Name": "John", "Department": "CE", "Year": "2018" },
{ "Name": "John", "Address": "Delhi", "Type": "Permanent" }

Expected Out-Put:

[{    
    "Name" : "SAM",   
    "Department": "CO",
    "Year": "2018"
    "children" : [{
                "Name": "SAM", 
                "Address": "Mumbai", 
                "Type": "Permanent"                
            }]
},
{    
    "Name" : "John",   
    "Department": "CE",
    "Year": "2018"
    "children" : [{
                "Name": "John", 
                "Address": "Delhi", 
                "Type": "Permanent"                
            }]
}]
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    Do you really want an external library to do this? – Federico klez Culloca Dec 14 '18 at 10:38
  • What have you tried so far? Hint: It's a pretty easy, but inefficient `Array.reduce()` loop. Add the children property, filter the original array for all those children, add them to the property, repeat. The irony being that the original array is easier to work with than once it's nested. – Shilly Dec 14 '18 at 10:42
  • Possible duplicate of [Hierarchical json from flat with parent ID](https://stackoverflow.com/questions/15376251/hierarchical-json-from-flat-with-parent-id) – Shilly Dec 14 '18 at 10:43
  • @FedericoklezCulloca Thank you .. Yes i Want external package like **npm xls-to-json** – Samadhan Virkar Dec 14 '18 at 12:37

1 Answers1

1

Here's the function you are looking

var arry = [{ "Id": "1", "Name": "abc", "Parent": "", "attr": "abc" },
           { "Id": "2", "Name": "abc", "Parent": "1", "attr": "abc" },
           { "Id": "3", "Name": "abc", "Parent": "2", "attr": "abc" },
           { "Id": "4", "Name": "abc", "Parent": "2", "attr": "abc" }];
function convert(array){
  var map = {};
  for(var i = 0; i < array.length; i++){
    var obj = array[i];
    obj.items= [];

    map[obj.Id] = obj;

    var parent = obj.Parent || '-';
    if(!map[parent]){
        map[parent] = {
            items: []
        };
    }
    map[parent].items.push(obj);
  }
  return map['-'].items;
}

var r = convert(arry);

Result

[{
    "Id" : "1",
    "Name" : "abc",
    "Parent" : "",
    "attr" : "abc",
    "children" : [{
                "Id" : "2",
                "Name" : "abc",
                "Parent" : "1",
                "attr" : "abc",
                "children" : [{
                            "Id" : "3",
                            "Name" : "abc",
                            "Parent" : "2",
                            "attr" : "abc",
                            "children" : []
                        }, {
                            "Id" : "4",
                            "Name" : "abc",
                            "Parent" : "2",
                            "attr" : "abc",
                            "children" : []
                        }]
            }]
}]

Copied from https://stackoverflow.com/a/15376430/1826429

Inderjeet Singh
  • 556
  • 1
  • 9
  • 22
  • Thank you .. You are right but i want npm package or middleware , So i can pass my json object to middleware and get expected result that will clean or reduce my code. like npm **xls-to-json** thank you very much... – Samadhan Virkar Dec 14 '18 at 12:34
  • Is there any middleware that conver Flat JSON to hirerachy JSON object. – Samadhan Virkar Dec 21 '18 at 06:27