DominicM
DominicM

Reputation: 6888

Restructure / rebuild json data tree

{
  "company": [
    { "region": [ "Europe", "Germany" ], "productLine": "Produce" },
    { "region": [ "Europe", "France" ], "productLine": "Produce" }
    ],
    "company2": [
    { "region": [ "Europe", "Germany" ], "productLine": "Produce" },
    {  "region": [ "Americas", "USA" ], "productLine": "Produce" }
    ]
}

With this json data how can I rebuild it so that I have Europe/Americas value as the primary(unique) node with Germany/France as it's children? company/company1 would be sub-children of France/Germany. I cant seem to figure out how to build arrays while keeping the relations correct. I essence I need to reverse the node tree.

Expected Output:

Tree structure like this:

-Europe
   -France
      -Company
      -Company2

I also need a special structure for a tree plugin:

var source = [ { label: "Europe", items: [
   {label: "France", items: [
      {label: "SuperShop", items: [
            {label: "Produce"}
         ]}
      ]
   }]
}]

What I need in the end is an Object array with value pair: label, items. Items being an object with sub-objects within.

Upvotes: 0

Views: 867

Answers (1)

miah
miah

Reputation: 10423

Obviously, I don't know why you need the new format, but it seems overly complex. If you have a large data set that you are looking through, you are going to take a hit on speed because, under it's current set up, you are going to have traverse over every element of the new array to find the one you are looking for ...

var inputs = {
  "company": [
    { "region": [ "Europe", "Germany" ], "productLine": "Produce" },
    { "region": [ "Europe", "France" ], "productLine": "Produce" }
    ],
    "company2": [
    { "region": [ "Europe", "Germany" ], "productLine": "Produce" },
    {  "region": [ "Americas", "USA" ], "productLine": "Produce" }
    ]
};

var converter = {};

// This new format requires a 2 step process to prevent it from being N^2
// So convert the input into a tree
//   Region
//     -> Country
//       -> Company
//         -> Array of Products
for(var company in inputs){
  for(var i = 0; i < inputs[company].length; i++){
    // Because the regions are an array of hashes it is simplest
    // to grab the value by using the previously gathered keys
    // and the key region
    var r = inputs[company][i]['region'];

    // Check if the region exists.  If not create it.
    if(!converter[r[0]]){
      converter[r[0]] = {};
    }
    // Check if the country exists.  If not create it.
    if(!converter[r[0]][r[1]]){
      converter[r[0]][r[1]] = {};
    }
    // Add the company to the array.
    if(!converter[r[0]][r[1]][company]){
      converter[r[0]][r[1]][company] = [];
    }
    converter[r[0]][r[1]][company].push(inputs[company][i]['productLine']);
  }
}

var outputs = [];

// Now walk converter and generate the desired object.
for( var region in converter){
  converted_region = {};
  converted_region["label"] = region;
  converted_region["items"] = [];
  for( var country in converter[region]){
    converted_country = {};
    converted_country["label"] = country;
    converted_country["items"] = [];
    for( var company in converter[region][country]){
      converted_company = {};
      converted_company["label"] = company;
      converted_company["items"] = [];
      for(var i = 0; i < converter[region][country][company].length; i++){
        converted_company["items"].push(converter[region][country][company][i]);
      }
      converted_country["items"].push(converted_company);
    }
    converted_region["items"].push(converted_country);
  }
  outputs.push(converted_region);
}

Upvotes: 3

Related Questions