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

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

PWHPWH Posts: 26Questions: 5Answers: 0
edited January 2020 in Free community support

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

  • kthorngrenkthorngren Posts: 20,276Questions: 26Answers: 4,765
    edited January 2020 Answer ✓

    The JSON response from that site looks like this:

    [{
        "BTC-ACM": {
            "initialprice": "0.00000304",
            "price": "0.00000343",
            "high": "0.00000343",
            "low": "0.00000285",
            "volume": "0.10078065",
            "bid": "0.00000332",
            "ask": "0.00000341"
        }
    }, {
        "BTC-AEON": {
            "initialprice": "0.00001553",
            "price": "0.00001567",
            "high": "0.00001681",
            "low": "0.00001553",
            "volume": "0.01842281",
            "bid": "0.00001561",
            "ask": "0.00001591"
        }
    },
    ....
    

    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. Then rows.add() and draw() 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

  • PWHPWH Posts: 26Questions: 5Answers: 0

    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

  • PWHPWH Posts: 26Questions: 5Answers: 0

    Your example works perfectly and I am able to replicate it on my local machine.
    Thank you very much for your kind help!!

  • kthorngrenkthorngren Posts: 20,276Questions: 26Answers: 4,765

    Glad to be of help.

    Kevin

This discussion has been closed.