Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
Getting data from JSON and formatting this into a table

I am getting a little stuck with this one. I am trying to use javascript on my website to convert my API JSON data to a table. Without using an array in an array of data in JSON, it works fine which each item on a new line but soon as I use an nested array of data in there, it has all the data on one line, seperated by a comma. Thinking a for loop would work here but i'm not 100% sure if this is the best method. I have tried multiple searches online and tested around myself but I can't seem to get it to work.

The following is simple version of the JSON data I am working with:

MACLIST = ["ABC","DEF"]
IPLIST = ["10.10.10.10","20.20.20.20"]
ZONELIST = ["Inside","Outside"]

var json = [{"MAC":MACLIST,"IP":IPLIST,"ZONE":ZONELIST},{"SOMETHING ELSE":"OK"}];

Script used:

$(document).ready(function () {
    ko.applyBindings({
        teams: json
    });
});

HTML used:

    <table>
    <thead>
        <tr>
            <th>Device</th>
            <th>IP</th>
            <th>Zone</th>
        </tr>
    </thead>
    <tbody data-bind="foreach: teams">
        <tr>
            <td data-bind="text: MAC"></td>
            <td data-bind="text: IP"></td>
            <td data-bind="text: ZONE"></td>
        </tr>
    </tbody>
</table>

Working JSON version (without nested array data) works:

var json = [{"MAC":"ABC","IP":"10.10.10.10","ZONE":"Inside"},{"MAC":"DEF","IP":"20.20.20.20","ZONE":"Outside}];

Any tips or advice would be greatly appreciated

Testing URL JSFiddle Link

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can map over each row to create a string of html for each row. Then inside each row, map over each column to create the string of html for each cell.

var json = [{
  "MAC": "ABC",
  "IP": "10.10.10.10",
  "ZONE": "Inside"
}, {
  "MAC": "DEF",
  "IP": "20.20.20.20",
  "ZONE": "Outside"
}];

const makeCell = (content) => {
  return `<td>${content}</td>`
}

const makeRow = (content) => {
  return `<tr>${content}</tr>`
}


const table = document.querySelector('.table')
const colHeaders = Object.keys(json[0]).map(key => makeCell(key)).join('')
const bodyRows = json.map(row => {
  return makeRow(Object.keys(row).map(col => makeCell(row[col])).join(''))
}).join('')


document.querySelector('thead tr').innerHTML = colHeaders
document.querySelector('tbody').innerHTML = bodyRows
<table>
  <thead>
    <tr></tr>
  </thead>
  <tbody>
  </tbody>
</table>

about 4 years ago · Juan Pablo Isaza Report

0

Hopefully this article will help you resolve the problem

https://www.geeksforgeeks.org/how-to-convert-json-data-to-a-html-table-using-javascript-jquery/

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!