Using javascript build custom table from json

Bharat Guru
Bharat Guru used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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

Author

Commented:
Is there way to do using javascript?
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

Author

Commented:
Thanks

Author

Commented:
Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial