Parsing Json Objects For HTML Table

Parsing JSON objects for HTML table

Loop over each object, appending a table row with the relevant data each iteration.

$(document).ready(function () {
$.getJSON(url,
function (json) {
var tr;
for (var i = 0; i < json.length; i++) {
tr = $('<tr/>');
tr.append("<td>" + json[i].User_Name + "</td>");
tr.append("<td>" + json[i].score + "</td>");
tr.append("<td>" + json[i].team + "</td>");
$('table').append(tr);
}
});
});

JSFiddle

Trying to parse JSON objects to HTML table

John , your JSON is invalid , this is the closest valid JSON i prepared from sample you shared above :

[
{
"id": "1",
"name": "John",
"type": "Client"
},
{
"id": "2",
"name": "Damen",
"type": "Agent"
},
{
"id": "3",
"name": "Aiden",
"type": "Admin"
}
]

Notice that for an array of objects 0 : {} format is invalid, so when you query it in a loop you're not receiving any result.

Incorrect :

   0:{
"id": "1",
"name": "John",
"type": "Client"
}

Correct :

    {
"id": "1",
"name": "John",
"type": "Client"
}

How to validate your JSON

Following sites will help you verify your json :

jsonlint

jsoneditoronline

Answer

Re-generate your JSON and then proceed as indicated in other answers.

Also , i would advice against parsing the result as string ! In my humble opinion, the proper way to do this is to return a valid json from Backend/server

JSON object data to html table

Assemble all the rows and insert it as one monolithic chunk of html.

Your data doesn't have the outer brackets, but I'm assuming it's an array of objects. (If it's originally provided as you present it, it's not valid JSON.)

const data = [{"PrekesID":656851,"Kodas":"6DUSA000000122","SandKodas1":"20402","Likutis_PP":0,"Likutis":11,"KAPKodas":"5902115799686","KAPPavad":"PRAUSTUVO VOŽTUVAS S","Kaina":40},{"PrekesID":656851,"Kodas":"6DUSA000000122","SandKodas1":"20406","Likutis_PP":11,"Likutis":11,"KAPKodas":"5902115799686","KAPPavad":"PRAUSTUVO VOŽTUVAS S","Kaina":40}];

const warehouseQuant = data =>
document.getElementById("myTable").innerHTML = data.map(
item => ([
'<tr>',
['Kodas','Kaina','Likutis'].map(
key => `<td>${item[key]}</td>`
),
'</tr>'
])
).flat(Infinity).join('');

warehouseQuant(data);
<table id="myTable"></table>

How to parse json into html table?

Just add </br> between the fields you want in the first cell:

tr.append("<td class=\"project_title\">" + obj.value[i].name + "</br>" + obj.value[i].attribute1 + "</br>" + obj.value[i].attribute2 + "</td>");

It may work in your case.

How to parse HTML table to JSON in one JSONObject?

You need to add the counter to the key of the object

$json['item']['name' + $i] = strip_tags($name);
$json['item']['status' + $i] = strip_tags($status);
$i++

parse line-separated json objects in html table

you don't need to split and map, it is to heavy for this case, try this

var myObj = JSON.parse(`[${this.responseText.replaceAll("}\n","},")}]`);

this is a little more complicated, but much more reliable,since it is not using any special symbols

var myObj = JSON.parse(`[{${this.responseText.substring(1).replaceAll("{",",{")}]`);

Parse html table data to JSON array in javascript

Here can be used array.prototype method to create arrays, and to create objects for each mydetails table I used object constructor. Map method creates a new array, forEach method - doesn't. At the end of the code, you can find 2 results (in JSON or in js format).

let list = (t) => {
return Array.prototype.slice.call(t);
}

function Table(date,target,mine,his){
this.date = date;
this.target = target;
this.mine = mine;
this.his = his;
}

const t = document.querySelectorAll('table.mydetails');
const dataArr = list(t);
const separate = dataArr.map((el)=>{
const obj = el.querySelectorAll('td');
return list(obj);
});
const finalArray = separate.map(element => {
const text = list(element).map(el => {
return el.innerText;
});
return text;
});


const result = finalArray.map(el=>{
const final = new Table(el[0],el[1],el[2],el[3]);
return final;
});
console.log(result)
const resultFinal = JSON.stringify(result);
console.log('Scrapped results', resultFinal);





Dynamic JSON object to Html Table

You can use for...in, Template literals and element.insertAdjacentHTML(position, text); to accomplish your task:

var data = {    "C#": 2172738,    "CSS": 9390,    "HTML": 135085,    "Java": 337323}for (key in data) {    var tr = `<tr><td>${key}</td><td>${data[key]}</td></tr>`;    document.querySelector('#_myTable tbody').insertAdjacentHTML('beforeend', tr);}
<table id="_myTable">    <thead>    <th>Language</th>    <th>Line of Codes</th>    </thead>    <tbody>    </tbody></table>

When Parsing JSON TO HTML Table Getting Undefined

You are getting the json as a string, you have to parse it before

Like this:

 var json = JSON.parse($("#productDiv").html());

Convert json data to a html table

Thanks all for your replies. I wrote one myself. Please note that this uses jQuery.

Code snippet:

var myList = [  { "name": "abc", "age": 50 },  { "age": "25", "hobby": "swimming" },  { "name": "xyz", "hobby": "programming" }];
// Builds the HTML Table out of myList.function buildHtmlTable(selector) { var columns = addAllColumnHeaders(myList, selector);
for (var i = 0; i < myList.length; i++) { var row$ = $('<tr/>'); for (var colIndex = 0; colIndex < columns.length; colIndex++) { var cellValue = myList[i][columns[colIndex]]; if (cellValue == null) cellValue = ""; row$.append($('<td/>').html(cellValue)); } $(selector).append(row$); }}
// Adds a header row to the table and returns the set of columns.// Need to do union of keys from all records as some records may not contain// all records.function addAllColumnHeaders(myList, selector) { var columnSet = []; var headerTr$ = $('<tr/>');
for (var i = 0; i < myList.length; i++) { var rowHash = myList[i]; for (var key in rowHash) { if ($.inArray(key, columnSet) == -1) { columnSet.push(key); headerTr$.append($('<th/>').html(key)); } } } $(selector).append(headerTr$);
return columnSet;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body onLoad="buildHtmlTable('#excelDataTable')"> <table id="excelDataTable" border="1"> </table></body>


Related Topics



Leave a reply



Submit