Search code examples
jqueryjsondogecoin-api

Displaying specific json data from URL


I'm trying to display the data for "total_txs" from this URL https://www.chain.so/api/v2/address/DOGE/DK1i69bM3M3GutjmHBTf7HLjT5o3naWBEk

I've put together the following but the div is blank. (Forgive me if its painfully simple to see what I'm doing wrong)

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

<script>
$.getJSON("https://www.chain.so/api/v2/address/DOGE/DK1i69bM3M3GutjmHBTf7HLjT5o3naWBEk", function(data) {

$('#totaldonations').text(data.total_txs);
});
</script>

<div id="totaldonations"></div> 

Solution

  • data further contains a key named data, under which your desired attribute lies. So you need to extract the value as data.data.total_txs.

    See the working demo below:

    $.getJSON("https://www.chain.so/api/v2/address/DOGE/DK1i69bM3M3GutjmHBTf7HLjT5o3naWBEk", function(data) {
      $('#totaldonations').text(data.data.total_txs);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="totaldonations"></div>

    To avoid any confusion, rename the callback argument to response, so the key becomes response.data.total_txs, which makes much more sense.