Vipul Singh
Vipul Singh

Reputation: 393

Manipulating JSON data using JavaScript for nested Object

I have recently started working in node JS and I am stuck in manipulating the JSON data. Given DATA is as :

"paymentTypeReason": [
    {
        "CATEGORY_NAME": "CDA Portfolio Submission Fee",
        "AMOUNT": 150,
        "GROUP_NAME": "",
        "SORT_ORDER": 100
    },
    {
        "CATEGORY_NAME": "Duplicate Diploma Request fee",
        "AMOUNT": 25,
        "GROUP_NAME": "",
        "SORT_ORDER": 200
    },
    {
        "CATEGORY_NAME": "Divine Nine Alliance (DNA)",
        "AMOUNT": 40,
        "GROUP_NAME": "Student Organizations",
        "SORT_ORDER": 300
    },
    {
        "CATEGORY_NAME": "Divine Nine Alliance (DNA) Annual Dues",
        "AMOUNT": 40,
        "GROUP_NAME": "Student Organizations",
        "SORT_ORDER": 400
    },
    {
        "CATEGORY_NAME": "Tau Upsilon for Human Services",
        "AMOUNT": 80,
        "GROUP_NAME": "Honor Societies",
        "SORT_ORDER": 700
    },
    {
        "CATEGORY_NAME": "Phi Alpha Honor Society",
        "AMOUNT": 60,
        "GROUP_NAME": "Honor Societies",
        "SORT_ORDER": 800
    }
]
}
}  

based on Group Name I have to create JSON Data as

"paymentTypeReason": [
    {
        "CATEGORY_NAME": "CDA Portfolio Submission Fee",
        "AMOUNT": 150,
        "GROUP_NAME": "",
        "SORT_ORDER": 100
    },
    {
        "CATEGORY_NAME": "Duplicate Diploma Request fee",
        "AMOUNT": 25,
        "GROUP_NAME": "",
        "SORT_ORDER": 200
    },
    {
        "categoryName": "Student Organizations",
        "groupName": null,
        "children": [
            {
                "CATEGORY_NAME": "Divine Nine Alliance (DNA)",
                "AMOUNT": 40,
                "GROUP_NAME": "Student Organizations",
                "SORT_ORDER": 300
            },
            {
                "CATEGORY_NAME": "Divine Nine Alliance (DNA) Annual Dues",
                "AMOUNT": 40,
                "GROUP_NAME": "Student Organizations",
                "SORT_ORDER": 400
            }
        ]
    },
    {
        "categoryName": "Honor Societies",
        "groupName": null,
        "sortOrder": 900,
        "children": [
            {
                "CATEGORY_NAME": "Tau Upsilon for Human Services",
                "AMOUNT": 80,
                "GROUP_NAME": "Honor Societies",
                "SORT_ORDER": 700
            },
            {
                "CATEGORY_NAME": "Phi Alpha Honor Society",
                "AMOUNT": 60,
                "GROUP_NAME": "Honor Societies",
                "SORT_ORDER": 800
            },
        ]
    },
]  

How to Achieve this using JavaScript. Being from Java Background I can do this using MAP, but facing difficulty it creating in JavaScript.
Here is code I have tried

for (paymentType of paymentTypes) { 
                        var paymentTypeReason ={};
                        paymentTypeReason.categoryName=paymentType.CATEGORY_NAME;
                        paymentTypeReason.amount = paymentType.AMOUNT;
                        paymentTypeReason.groupName = paymentType.GROUP_NAME;                       
                        paymentTypeReason.sortOrder = paymentType.SORT_ORDER;                                             
                        paymentTypeReason.children = children;
                       // console.log(paymentTypeReason);
                         if(null == paymentTypeReason.groupName || "" == paymentTypeReason.groupName){  
                             // console.log(paymentType);                                                         
                             resultReasons.push(paymentTypeReason);
                         } else{ 
                             let childArray = []; 
                             console.log(reasonMap);
                                if(reasonMap.has(paymentTypeReason.groupName)){
                                    console.log("inside if");
                                    console.log(reasonMap)
                                    childArray=reasonMap[paymentTypeReason.groupName]
                                } else{
                                    resultReasons.push({
                                        "categoryName" : paymentTypeReason.groupName,
                                        "groupName": null,
                                        "sortOrder": paymentTypeReason.sortOrder,
                                        "children" : []
                                    })
                                }
                                childArray.push(paymentTypeReason);
                                reasonMap[paymentTypeReason.groupName] = childArray                                       
                         }
                     }  

Upvotes: 0

Views: 60

Answers (2)

Giovanni Esposito
Giovanni Esposito

Reputation: 11156

Ciao, try in this way:

let input = {"paymentTypeReason": [
            {
                "CATEGORY_NAME": "CDA Portfolio Submission Fee",
                "AMOUNT": 150,
                "GROUP_NAME": "",
                "SORT_ORDER": 100
            },
            {
                "CATEGORY_NAME": "Duplicate Diploma Request fee",
                "AMOUNT": 25,
                "GROUP_NAME": "",
                "SORT_ORDER": 200
            },
            {
                "CATEGORY_NAME": "Divine Nine Alliance (DNA)",
                "AMOUNT": 40,
                "GROUP_NAME": "Student Organizations",
                "SORT_ORDER": 300
            },
            {
                "CATEGORY_NAME": "Divine Nine Alliance (DNA) Annual Dues",
                "AMOUNT": 40,
                "GROUP_NAME": "Student Organizations",
                "SORT_ORDER": 400
            },
            {
                "CATEGORY_NAME": "Tau Upsilon for Human Services",
                "AMOUNT": 80,
                "GROUP_NAME": "Honor Societies",
                "SORT_ORDER": 700
            },
            {
                "CATEGORY_NAME": "Phi Alpha Honor Society",
                "AMOUNT": 60,
                "GROUP_NAME": "Honor Societies",
                "SORT_ORDER": 800
            }
        ]
        }
        
        let groupnames = [...new Set(input.paymentTypeReason.map(el => {
            return el.GROUP_NAME;
        }))]; 
        
         let result = {};
         result.paymentTypeReason = [];
        groupnames.forEach(group => {
           let filter = input.paymentTypeReason.filter(el => el.GROUP_NAME === group);
           if (filter.length > 0) {
              if (filter[0].GROUP_NAME !== "") {
                 let resobj = {};
                 resobj.groupName = null;
                 resobj.categoryName = group;
                 resobj.children = filter;
                 result.paymentTypeReason.push(resobj)
              }
              else {
                 filter.forEach(el => result.paymentTypeReason.push(el))
              }
           }
           
        })
        
        console.log(result)

First I get distinct GROUP_NAME. Then for each group I filter input.paymentTypeReason array and finally I had the element found in children array (except for GROUP_NAME equal to ""). The only think I don't undertand is how to create sortOrder.

Upvotes: 1

pintxo
pintxo

Reputation: 2155

This is not a complete answer, but should give you the right directions:

const input = { 
  "paymentTypeReason": [
    {
        "CATEGORY_NAME": "CDA Portfolio Submission Fee",
        "AMOUNT": 150,
        "GROUP_NAME": "",
        "SORT_ORDER": 100
    },
    {
        "CATEGORY_NAME": "Duplicate Diploma Request fee",
        "AMOUNT": 25,
        "GROUP_NAME": "",
        "SORT_ORDER": 200
    },
    {
        "CATEGORY_NAME": "Divine Nine Alliance (DNA)",
        "AMOUNT": 40,
        "GROUP_NAME": "Student Organizations",
        "SORT_ORDER": 300
    },
    {
        "CATEGORY_NAME": "Divine Nine Alliance (DNA) Annual Dues",
        "AMOUNT": 40,
        "GROUP_NAME": "Student Organizations",
        "SORT_ORDER": 400
    },
    {
        "CATEGORY_NAME": "Tau Upsilon for Human Services",
        "AMOUNT": 80,
        "GROUP_NAME": "Honor Societies",
        "SORT_ORDER": 700
    },
    {
        "CATEGORY_NAME": "Phi Alpha Honor Society",
        "AMOUNT": 60,
        "GROUP_NAME": "Honor Societies",
        "SORT_ORDER": 800
    }
  ]
};

const grouped = input.paymentTypeReason.reduce((grouped, item) => {
  const category = grouped.find(g => g.GROUP_NAME === item.GROUP_NAME);
  if (category) {
    category.children.push(item);
  } else {
    grouped.push({
      GROUP_NAME: item.GROUP_NAME,
      children: [item],
    });
  }
  return grouped;  
}, []);

document.querySelector('#console').textContent = JSON.stringify(grouped, null, 4)
<pre id="console"></pre>

Upvotes: 1

Related Questions