Skip to content
Loading
  • Jignesh Kumar
    0
    Hello,
     
    Please try belo link which will help you,
    https://codereview.stackexchange.com/questions/14429/convert-json-to-a-different-format-nested-json 
    0
  • Rajanikant Hawaldar
    0
    Try this const restructureProducts = (oldProds) => { let products = []; let newProd = {}; if (oldProds.hasOwnProperty('ProductID')) newProd['ProductID'] = oldProds['ProductID']; if ( oldProds.hasOwnProperty('Category') && oldProds['Category'].length > 0 && oldProds['Category'][0].hasOwnProperty('CategoryID') ) newProd['Category'] = { CategoryID: oldProds['Category'][0]['CategoryID'] }; if (oldProds['Category'][0].hasOwnProperty('SubCategory') && oldProds['Category'][0]['SubCategory'].length > 0) newProd['Category']['SubCategory'] = { SubCategoryID: oldProds['Category'][0]['SubCategory'][0]['SubCategoryID'] }; products.push(newProd); return products; } let oldProds = { ProductID: 1, Category: [{ CategoryID: 1, SubCategory: [{ SubCategoryID: 1, }] }] }; let products = restructureProducts(oldProds); console.log(products);
    0