Enkhtuvshin
Enkhtuvshin

Reputation: 67

Get specific value from json with jquery

I'm trying to get a data from http://api.coindesk.com/v1/bpi/currentprice/usd.json here which looks like this

{
    "time": {
        "updated": "Jan 18, 2017 02:55:00 UTC",
        "updatedISO": "2017-01-18T02:55:00+00:00",
        "updateduk": "Jan 18, 2017 at 02:55 GMT"
    },
    "disclaimer": "This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org",
    "bpi": {
        "USD": {
            "code": "USD",
            "rate": "888.9525",
            "description": "United States Dollar",
            "rate_float": 888.9525
        }
    }
}

and the data i want to get is only "bpi"."USD"."rate" part but i'm unavailable to do so.

$.getJSON("demo_ajax_json.js", function(result){
    $.each(result, function(i, field){
        $("div").append(field + " ");
    });
});

I'm trying to put it here but i'm having a problems because of "stacked?" keys and values. Can someone direct me to the right way? If i understand correctly i have JSON.parse but still i don't completely understand it

Upvotes: 0

Views: 3135

Answers (2)

Abdul Mateen Mohammed
Abdul Mateen Mohammed

Reputation: 1894

As you're getting a valid JavaScript object from the $.getJSON call you can access the property you want this way

data.bpi.USD.rate

Check the below code snippet

$(document).ready(function() {
  $.getJSON('http://api.coindesk.com/v1/bpi/currentprice/usd.json')
    .success(getCurrentPriceCompleted)
    .error(getCurrentPriceFailed);

  function getCurrentPriceCompleted(data) {
    console.log(data.bpi.USD.rate);
  }

  function getCurrentPriceFailed(error) {
    console.log(error);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<fieldset>
  <legend>The JavaScript Object (response from server):</legend>
  <code>
  {
   "time":{
      "updated":"Jan 18, 2017 02:56:00 UTC",
      "updatedISO":"2017-01-18T02:56:00+00:00",
      "updateduk":"Jan 18, 2017 at 02:56 GMT"
   },
   "disclaimer":"This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org",
   "bpi":{
      "USD":{
         "code":"USD",
         "rate":"888.7975",
         "description":"United States Dollar",
         "rate_float":888.7975
      }
   }
}
  </code>
</fieldset>

Upvotes: 2

guradio
guradio

Reputation: 15565

var data = {
	"time": {
		"updated": "Jan 18, 2017 02:55:00 UTC",
		"updatedISO": "2017-01-18T02:55:00+00:00",
		"updateduk": "Jan 18, 2017 at 02:55 GMT"
	},
	"disclaimer": "This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org",
	"bpi": {
		"USD": {
			"code": "USD",
			"rate": "888.9525",
			"description": "United States Dollar",
			"rate_float": 888.9525
		}
	}
};


console.log(data.bpi.USD.rate)

Use like this

Upvotes: 1

Related Questions