Using javascript build custom table from json

How to build table using javascript for below JSON , I want to display Total for each location and RecordType first and then detail for location and recordtype and add extra blank row for each different category


{
   "action":"query",
   "Warehouse":"NJ",
   "records":[
      {
         "Location":"UK",
         "RecordType":"Fruits",
         "Qty":16,
         "Name":"Total"
      },
      {
         "Location":"UK",
         "RecordType":"Grocery",
         "Qty":11,
         "Name":"Total"
      },
      {
         "Location":"US",
         "RecordType":"Fruits",
         "Qty":5,
         "Name":"Total"
      },
        {
         "Location":"US",
         "RecordType":"Grocery",
         "Qty":17,
         "Name":"Total"
      },
      {
         "Location":"US",
         "RecordType":"Fruits",
         "Productid":123,
         "Qty":5,
         "Name":"Apple"
      },
      {
         "Location":"UK",
         "RecordType":"Fruits",
         "Productid":124,
         "Qty":11,
         "Name":"Bananna"
      },

      {
         "Location":"US",
         "RecordType":"Grocery",
         "Productid":223,
         "Qty":6,
         "Name":"Rice"
      },
      {
         "Location":"UK",
         "RecordType":"Grocery",
         "Productid":224,
         "Qty":11,
         "Name":"Beans"
      }
   ]
}

table
Location  Name   RecordType    ProductID, Qty
US        ,Total   ,Grocery    ,          ,6
US        ,Rice   ,Grocery     ,223       ,6

US        ,Total   ,Fruits    ,          ,5
US        ,Apple   ,Fruits    ,123       ,5

UK        ,Total   ,Grocery     ,          ,11
UK        ,Beans   ,Grocery    ,224       ,11

UK        ,Total   ,Fruits     ,          ,11
UK        ,Bananna ,Fruits     ,124       ,11
Bharat GuruAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

InsoftserviceCommented:
use datatable it would be easier to build .
You can have search ,sort,pagination feature in built in that.

https://editor.datatables.net/examples/advanced/deepObjects.html
Bharat GuruAuthor Commented:
Is there way to do using javascript?
Slick812Commented:
greetings Bharat Guru ??  Not sure about the way you want this Table to be displayed ??
 But here is my attempt to do Javascript Code and Html to read your JSON Object and make a Table with ALL entries in the the "records" array

<style>
#dft {background: #dfe; display: inline-block; border:1px solid #7b6; padding: 7px;}

#dft thead {background: #fff; color: #888;}

#dft th {border:2px solid #aaa; padding: 4px; text-align:center;}

#dft td {border:1px solid #000; padding: 4px; text-align:center;}
</style>

<div id="dft"></div>

<script>
var dft = {
   action:"query",
   Warehouse:"NJ",
   records:[
      {
         Location:"UK",
         RecordType:"Fruits",
         Qty:16,
         Name:"Total"
      },
      {
         Location:"UK",
         RecordType:"Grocery",
         Qty:11,
         Name:"Total"
      },
      {
         Location:"US",
         RecordType:"Fruits",
         Qty:5,
         Name:"Total"
      },
        {
         Location:"US",
         RecordType:"Grocery",
         Qty:17,
         Name:"Total"
      },
      {
         Location:"US",
         RecordType:"Fruits",
         Productid:123,
         Qty:5,
         Name:"Apple"
      },
      {
         Location:"UK",
         RecordType:"Fruits",
         Productid:124,
         Qty:11,
         Name:"Bananna"
      },

      {
         Location:"US",
         RecordType:"Grocery",
         Productid:223,
         Qty:6,
         Name:"Rice"
      },
      {
         Location:"UK",
         RecordType:"Grocery",
         Productid:224,
         Qty:11,
         Name:"Beans"
      }
   ]
}

var tlbStr1 = tlbStr2 = "";
for(var i=0;i<dft.records.length;i++) { var pids = dft.records[i].Productid ? dft.records[i].Productid : '-';
    if (dft.records[i].Location == "US") {
    tlbStr1 += "<tr><td>"+dft.records[i].Location+"</td><td>"+dft.records[i].Name+
              "</td><td>"+dft.records[i].RecordType+"</td><td>"+pids+
              "</td><td>"+dft.records[i].Qty+"</td></tr>";
    }
     else if (dft.records[i].Location == "UK") {
    tlbStr2 += "<tr><td>"+dft.records[i].Location+"</td><td>"+dft.records[i].Name+
              "</td><td>"+dft.records[i].RecordType+"</td><td>"+pids+
              "</td><td>"+dft.records[i].Qty+"</td></tr>";
    }
    }
    
tlbStr1 += "<tr><td columnspan='5' style='border:0'>&nbsp;</td></tr>";
tlbStr2 += "</tbody></table>";

document.getElementById("dft").innerHTML = "<table><thead><tr><th>Location</th><th>Name</th>"+
    "<th>RecordType</th><th>ProductID</th><th>Qty</th><tr></thead><tbody>"+tlbStr1+tlbStr2;
    
</script>

Open in new window


This code works for me and shows a Table with
     Location      Name      RecordType      ProductID      Qty

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bharat GuruAuthor Commented:
Thanks
Bharat GuruAuthor Commented:
Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.