As from the table to do JSON?

<table id="print-table">
 <tbody><tr class="main-head-table">
<th>name</th>
<th>Position</th>
 <th>extension number</th>
</tr>
the <tr>
 <td colspan="7" style="text-align: center; font-weight: bold;" class="hide-tr">Administration</td>
</tr><tr>
<td>wer</td>
<td>sdf</td>
<td>10001</td>
</tr><tr>
<td>sdf</td>
<td>sdf</td>
<td>10002</td>
</tr>
 </tr></tbody></table>

As in the table to make this json
someJSONdata = [
{
name: 'John Doe',
rank: 'john@doe.com',
phone: '111-111-1111'
},
{
name: 'Barry Allen',
rank: 'barry@flash.com',
phone: '222-222-2222'
},
{
name: 'Cool Dude',
rank: 'cool@dude.com',
phone: '333-333-3333'
}
]
April 3rd 20 at 17:47
4 answers
April 3rd 20 at 17:49
Solution
Without JQuery:

tableToJson function() {
 let result = [];
 let table = document.getElementById("print-table").getElementsByTagName("tbody")[0];
 let trs = table.getElementsByTagName("tr");
 for (let i = 2; i < trs.length; i++) {
 let tds = trs[i].getElementsByTagName("td");
 let obj = {};
 obj.name = tds[0].innerHTML;
 obj.rank = tds[1].innerHTML;
 obj.phone = tds[2].innerHTML;
result.push(obj);
}
 return JSON.stringify(result);
}
April 3rd 20 at 17:51
Solution
Probecause tr in the tbody and get the dictionary. The first tag in jQuery - so, probably realize that it's each and push for the finished dictionary.

UPD:
Saw that all sad - so nya:

var json_table = [];
$('#print-table tbody tr').slice(2).each(function() {
 var tds = $(this).children('td');
json_table.push({
 name: tds[0].text(),
 rank: tds[1].text(),
 phone: tds[2].text(),
});
});

Chet something, debug there, wrote in the blind.
Need to be pruned, because the first two tr - Bertrand_Pfeffer commented on April 3rd 20 at 17:54
@Bertrand_Pfeffer, but I missed, corrected as slice - Jarrod_Muller commented on April 3rd 20 at 17:57
April 3rd 20 at 17:53
Solution
Easy if you try.

const dataRows = document.querySelectorAll('#print-table tr:nth-child(n+2)');
const jsonStruct = [];

dataRows.forEach(row => {
 let fields = row.querySelectorAll("td");
 if (fields.length !== 3) return;

jsonStruct.push({
 name: fields[0].textContent,
 rank: fields[1].textContent,
 phone: fields[2].textContent
});
});

console.log(jsonStruct);


I tried, did not work
const data = [];// Response
 const nameTd = [];
 const Table = $('#print-table');
 const numberTr = Table.find('tr').length;
 const numberTd = Table.find('tr:first > td').length;

 for (let i = 1; i <= numberTd; i++) {
 nameTd.push(Table.find(`tr:first-child > td:nth-child(${i})`).text());
}

 for (let i = 2; i <= numberTr; i++) {
 const objectTd = {};

 for (let k = 0; k < nameTd.length; k++) {
 objectTd[nameTd[k]] = Table.find(`tr:nth-child(${i})>td:nth-child(${k + 1})`).text();
}

data.push(objectTd);
 }
- ellsworth.Ko commented on April 3rd 20 at 17:56
@ellsworth.Ko, omg))) - Jarrod_Muller commented on April 3rd 20 at 17:59
@ellsworth.Ko, wait till I work to get there. - Bertrand_Pfeffer commented on April 3rd 20 at 18:02
@Bertrand_Pfeffer, Yes it is gg unreadable)) don't worry, I threw in above. - Jarrod_Muller commented on April 3rd 20 at 18:05
@ellsworth.Ko, updated. - Bertrand_Pfeffer commented on April 3rd 20 at 18:08
April 3rd 20 at 17:55
Solution
Variant without jQuery, maybe someone will be interested - https://jsfiddle.net/56ob7uye/9/

Find more questions by tags JavaScriptjQueryJSON