Newbie question; How do I format an API to display data?
Newbie question; How do I format an API to display data?

I am completely new to this, a hobby for my retirement!.
I have downloaded and have the example datatable running on my local computer. It's just what I need.
Now, I want to change the data source API but when I do, I can't get the data to display in the table.
The only file I have changed is main.js. If I simply change the API, I get a " Loading..." message in the table display and a console error "TypeError: d is undefined". I can see if I load the API's directly and look via the console, that the layout of the two API's is different. The example one is 'neat' whereas the API I want is not.
I have tried all the examples I think will work but to no avail. The only code I can get to display the API via the console is
var table = $('datatable').DataTable();
$.getJSON("https://tradeogre.com/api/v1/markets", function(data) {
for (var i=0; i<data.length; i++)
for (var name in data[i]) {
console.log("Item name: "+name);
console.log("Price: "+data[i][name].price);
console.log("Initial Price: "+data[i][name].initialprice);
}
});
});
But then, I can't get the information to display on the table.
I hope that makes sense. Please can you point me in the right direction.
Thank you
This question has an accepted answers - jump to answer
Answers
The JSON response from that site looks like this:
This structure, while valid JSON, is not supported by Datatables. Supported structures are documented here:
https://datatables.net/manual/data/
You will need to iterate the response to restructure it. This example shows one way to do this:
http://live.datatables.net/sepevazu/1/edit
You will also need to add
columns.data
since the data is object based. Thenrows.add()
anddraw()
are used to add the rows to the Datatable and draw them.Note: The triple ticks you used for Markdown need to be on separate lines. I edited your post to change this.
Kevin
Thank you very much Kevin!
I don't feel quite so bad now I know the structure is not supported.
I'll have a go with your suggestions and let you know how I get on
Your example works perfectly and I am able to replicate it on my local machine.
Thank you very much for your kind help!!
Glad to be of help.
Kevin