How can i output a multilevel json object ?
How can i output a multilevel json object ?
mdesign
Posts: 72Questions: 17Answers: 0
How can i output a multilevel json object. can anyone help me with the correct syntax für the columns. see this simple case for example.
/* table rows should look like this */
Summer 2024 | Team Blue 1 | Nr.1 | Hans
Summer 2024 | Team Blue 1 | Nr.2 | Noah
Summer 2024 | Team Pink 2 | Nr.1 | Emma
Summer 2024 | Team Pink 2 | Nr.2 | Susi
<table id="example">
<thead>
<tr>
<th>seasonName</th>
<th>contestType</th>
<th>teamNr</th>
<th>fedRank</th>
<th>firstname</th>
</tr>
</thead>
<tbody></tbody>
</table>
<script>
var thisDataTable = $('#example').DataTable({
ajax: 'file.json',
columns: [ ??? ],
}); // thisDataTable
</script>
/* file.json */
{
"data": [
{
"seasonName": "Summer 2024",
"teams": [
{
"contestType": "Team Blue",
"players": [
{
"rank": 1,
"firstname": "Hans",
},
{
"rank": 2,
"firstname": "Noah",
},
]
},
{
"contestType": "Team Pink",
"players": [
{
"rank": 1,
"firstname": "Emma",
},
{
"rank": 2,
"firstname": "Susi",
},
]
},
]
}
]
}
Answers
If you want that output, you'd need to preprocess the JSON data to fold it down. DataTables will display one row per entry in the data array that it is given to show. It cannot show nested data like that over multiple rows.
Allan
thx allan, that saves me time