0

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

2
  • What do you get in result? Commented Jan 18, 2017 at 2:56
  • I got this [object Object] This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org [object Object] @A.J Commented Jan 18, 2017 at 2:58

2 Answers 2

2

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>

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.