0

This is a nested json file and I am trying to arrange it in a readable format to display in a table

I tried to manually put all the keys and values with in a for loop but there should be an elegant way to achieve this and hence I am reaching SO.

The actual JSON is quite a nested one and needed time to execute data with 500k rows

The result should be enhanced JSON with parent values appearing for child values as well

var property = {
 "data": [{
  "ID": "123456",
  "name": "Coleridge st",
  "criteria": [
   {
    "type": "type1",
    "name": "name1",
    "value": "7",
    "properties": []
   },
   {
    "type": "type2",
    "name": "name2",
    "value": "6",
    "properties": [
     {
      "type": "MAX",
      "name": "one",
      "value": "100"
     }, {
      "type": "MIN",
      "name": "five",
      "value": "5"
     }

    ]
   },
   {
    "type": "type3",
    "name": "name3",
    "value": "5",
    "properties": [{
     "type": "MAX1",
     "name": "one6",
     "value": "1006"
    }, {
     "type": "MIN2",
     "name": "five6",
     "value": "56"
    }]
   }
  ]
 },
 {
  "ID": "456789",
  "name": "New Jersy",
  "criteria": [
   {
    "type": "type4",
    "name": "name4",
    "value": "6",
    "properties": [{
     "type": "MAX12",
     "name": "one12",
     "value": "10012"
    }, {
     "type": "MIN23",
     "name": "five12",
     "value": "532"
    }]
   }
  ]
 }]
};

var output = [];
property.data.forEach(function (users) {

 var multirows = {
  id: users.ID,
  name: users.name,
 };

 for (var i = 0; i < users.criteria.length; i++) {
  var criterias = {
   type: users.criteria[i].type,
   name: users.criteria[i].name,
   value: users.criteria[i].value,
  }

  var mat_contacts_rows;
  if (!isEmpty(users.criteria[i].properties)) {
   for (var j = 0; j < users.criteria[i].properties.length; j++) {
    var property = {
     type: users.criteria[i].properties[j].type,
     name: users.criteria[i].properties[j].name,
     value: users.criteria[i].properties[j].value
    };

    mat_contacts_rows = { ...multirows, ...{ criteria: criterias }, ...{ properties: property } };
    output.push(mat_contacts_rows);
   }
  } else {
   var property = [];
   mat_contacts_rows = { ...multirows, ...{ criteria: criterias }, ...{ properties: property } };
   output.push(mat_contacts_rows);
  }
 }
});

console.log(JSON.stringify(output, undefined, 2))


function isEmpty(obj) {
 for (var key in obj) {
  if (obj.hasOwnProperty(key))
   return false;
 }
 return true;
}
Narik
  • 166
  • 1
  • 7

1 Answers1

1

I think this could be a great exercise to you to don't answer your question but to give you some tips. You should first look at : Lodash wish has a bunch of usefull method to help you doing what you'r trying to do. In a second time you should avoir using .forEach or for loops and try using Array.prototype.map or Array.prototype.reduce

Jeremy M.
  • 1,154
  • 1
  • 9
  • 29
  • There should be some sort of generic function to check if it is an object or array and then flatten it.something like this link https://stackoverflow.com/questions/19098797/fastest-way-to-flatten-un-flatten-nested-json-objects – Narik Jun 23 '19 at 17:37