3

EDIT - What is the purpose of marking a question as duplicate? Earn some points and get a little wiseass kick? You cannot know that this question will not help someone. The answers to the question that this "duplicated" would not have answered my question in my limited knowledge, but the gentleman who answered MY question did. -EDIT

I am looking to select just one value from a list of JSON data, the 24 hr price percentage change:

{
    "id": "stellar", 
    "name": "Stellar", 
    "symbol": "XLM", 
    "rank": "6", 
    "price_usd": "0.570132", 
    "price_btc": "0.00005009", 
    "24h_volume_usd": "672209000.0", 
    "market_cap_usd": "10187093680.0", 
    "available_supply": "17867956333.0", 
    "total_supply": "103629819514", 
    "max_supply": null, 
    "percent_change_1h": "1.8", 
    "percent_change_24h": "16.65", 
    "percent_change_7d": "23.95", 
    "last_updated": "1516839244"
} 

and at the moment, my current code, which is just to test if what I have thus far works at all, returns simply [object Object] :

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
 $(document).ready(function () {
  $.getJSON('https://api.coinmarketcap.com/v1/ticker/stellar/',
  function (data) {
   document.body.append(data);
  });
 });
</script>

I want to isolate - and to start, just to show - only that "percent_change_24h" and work from there.

Thank you.

Alb
  • 221
  • 2
  • 5
  • 14

2 Answers2

4

Well, you can access directly using that key percent_change_24h:

var data = {
  "id": "stellar",
  "name": "Stellar",
  "symbol": "XLM",
  "rank": "6",
  "price_usd": "0.570132",
  "price_btc": "0.00005009",
  "24h_volume_usd": "672209000.0",
  "market_cap_usd": "10187093680.0",
  "available_supply": "17867956333.0",
  "total_supply": "103629819514",
  "max_supply": null,
  "percent_change_1h": "1.8",
  "percent_change_24h": "16.65",
  "percent_change_7d": "23.95",
  "last_updated": "1516839244"
};

console.log(data['percent_change_24h']);
document.body.append(data['percent_change_24h']);
// in your case document.body.append(data['percent_change_24h']);

Hope it helps!

Ele
  • 33,468
  • 7
  • 37
  • 75
3

https://api.coinmarketcap.com/v1/ticker/stellar/ returns the array:

[
    {
        "id": "stellar", 
        "name": "Stellar", 
        "symbol": "XLM", 
        "rank": "6", 
        "price_usd": "0.566242", 
        "price_btc": "0.00004991", 
        "24h_volume_usd": "674523000.0", 
        "market_cap_usd": "10117586651.0", 
        "available_supply": "17867955133.0", 
        "total_supply": "103629819514", 
        "max_supply": null, 
        "percent_change_1h": "-0.26", 
        "percent_change_24h": "16.45", 
        "percent_change_7d": "21.53", 
        "last_updated": "1516840744"
    }
]

So in order to get access to the percent_change_24h field you need data[0].percent_change_24h

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
                $.getJSON('https://api.coinmarketcap.com/v1/ticker/stellar/',
                function (data) {
                    document.body.append(data[0].percent_change_24h);
                });
        });
    </script>
Kirill Simonov
  • 8,257
  • 3
  • 18
  • 42