2

I need output data in JSON array format to use with jQuery DataTables but my API from Apigility gives HAL+JSON. How can I convert it so that I can pass it from fetchAll method of API to AJAX URL of Datatables?

Here is what the HAL+JSON looks like:

{"_links":{"self":{"href":"http:\/\/parekh.com\/exportmanager\/courses"}},"_embedded":{"courses":[{"Module Title":"Preventing Harassment","Module ID":"HUR602","Module Language":"English"},{"Module Title":"Conflict","Module ID":"COM236","Module Language":"Dutch"},{"Module Title":"Workplace","Module ID":"HUR711","Module Language":"Dutch"}]},"total_items":3}
Gyrocode.com
  • 57,606
  • 14
  • 150
  • 185
Prateek
  • 95
  • 2
  • 13

1 Answers1

4

SOLUTION

No need to convert the format of the data, jQuery DataTables is able to read complex structures when correct options are used.

Use ajax.dataSrc option to define data source for the table and columns.data option to set data source for each column.

var table = $('#example').DataTable({
    ajax: {
        url: 'https://api.myjson.com/bins/3ebaq',
        dataSrc: '_embedded.courses'
    },
    columns: [
        { data: 'Module Title' },
        { data: 'Module ID' },
        { data: 'Module Language' }
    ]
});

DEMO

See this jsFiddle for code and demonstration.

Gyrocode.com
  • 57,606
  • 14
  • 150
  • 185