How to analyze JSON received from ajax call Datatables?

advertisements

I can successfully fill my datatable with ajax call, but then I don't know how to parse JSON that is received by datatable with this ajax call.

Here is my JavaScript code, that makes ajax call to the server and fills my datatable correctly:

$('#transactions').DataTable({
        "processing": true,
        "ajax": {
            "url": "/transactions
        },
        "columns": [
            { "data": "car"},
            { "data": "card_number"},
            { "data": "invoice"},
            { "data": "status"}
        ]
    });

This is the JSON object returned from the server:

{
  "data": [
    {
      "car": 190,
      "card_number": "6395637",
      "invoice": 200,
      "status": "success"
    },
    {
      "car": 191,
      "card_number": "9473650",
      "invoice": 180,
      "status": "success"
    }
  ],
  "balance": 7300
}

As you can see, the data parameter of the returned JSON object is used by the datatables function to fill by datatables, and now I want to parse the balance parameter, but I can't. How can i achieve this?


Something like this:

$('#transactions').dataTable({
    "ajax" : {
        "url" : "/transactions",
        "dataSrc" : function (json) {
            // manipulate your data (json)
            ...

            // return the data that DataTables is to use to draw the table
            return json.data;
        }
    }
});

Docs: https://datatables.net/reference/option/ajax.dataSrc