Datatables with JSON of objects and array

Datatables with JSON of objects and array

albertodentealbertodente Posts: 3Questions: 2Answers: 0

Hello, greetings to all.
I am very new to datatables and how to represent a JSON, so I resort to the experience of those who know.

I have a server that returns data in JSON and I want to show them in a table with these columns:

<td>Time</td>
<td>hostname</td>
<td>cpu</td>
<td>mem</td>
<td>load</td>
<td>disk</td>

So I call the JSON response:

$(document).ready(function() {
var table = $('#example').DataTable( {
"ajax": {
"url": "http://les000/query?db=tele&q=SELECT LAST(cpu_used) AS cpu, LAST(mem_used) AS mem, LAST(load) AS load, LAST(disk_await) AS disk_await FROM custom GROUP BY hostname ORDER BY time",
"dataType": "json",
"cache": false,
"contentType": "application/json; charset=utf-8"
deferRender: true,
columns: [
{data: 'time' },
{ data: 'hostname' },
{ data: 'cpu' },
{ data: 'mem' },
{ data: 'load' },
{ data: 'disk' }
],
rowId: 'extn',
select: true,
dom: 'Bfrtip',
buttons: [
{
text: 'Reload table',
action: function () {
table.ajax.reload();
}
}
]
} );
} );

I get the following answer:

Método de la petición: GET
Código de estado: HTTP/1.1 200 OK

And this the JSON that I get:

{
"results": [{
"statement_id": 0,
"series": [{
"name": "custom",
"tags": {
"hostname": "LINUX2345"
},
"columns": ["time", "cpu", "mem", "load", "disk_await"],
"values": [
["1970-01-01T00:00:00Z", 1, 78, 0, 0]
]
}, {
"name": "custom",
"tags": {
"hostname": "LINUX344334"
},
"columns": ["time", "cpu", "mem", "load", "disk_await"],
"values": [
["1970-01-01T00:00:00Z", 9, 49, 1, 0]
]
}]
}]
}

But in the table I have nothing.

Can someone please guide me? Thank you.

Answers

  • asystemsausasystemsaus Posts: 15Questions: 5Answers: 0

    Hi, It looks like you dont have a ' type' in your ajax call. It needs to to be able tell whether to POST or GET your request.
    you should have something like

    url : url,
    data, data,
    "dataType": "json",
    "type" : "GET",
    "cache": false,
    

    I always try to make the data which i'm passing to a php easy to edit and read. So if you assign it as a variable earlier you can make your code just a little easier to read.
    Hope this helps.

  • albertodentealbertodente Posts: 3Questions: 2Answers: 0

    Thanks for answering.
    I do not think that's the problem because if it returns a valid JOSN. I think the problem is in making reference to the JSON element, for example in this I do not think it's okay:

    {data: 'time' },

    To make reference to this:

    {
    "results": [{
    "statement_id": 0,
    "series": [{
    "name": "custom",
    "tags": {
    "hostname": "LINUX2345"
    },
    "columns": ["time", "cpu", "mem", "load", "disk_await"],
    "values": [
    ["1970-01-01T00:00:00Z", 1, 78, 0, 0]
    ]
    }, {
    "name": "custom",
    "tags": {
    "hostname": "LINUX344334"
    },
    "columns": ["time", "cpu", "mem", "load", "disk_await"],
    "values": [
    ["1970-01-01T00:00:00Z", 9, 49, 1, 0]
    ]
    }]
    }]
    }

This discussion has been closed.