-6

I'm using the Coindesk JSON API to get the latest Bitcoin exchange rate.

http://api.coindesk.com/v1/bpi/currentprice/GBP.json

I want to display the exchange rate, eg: "284.0493" in plain text on a page on my site. What is the correct way to extract the correct value from the JSON array and display it in a HTML div?

Any solution using JavaScript, PHP or even Python will be fine.

So far I've been working from this.

Thanks!

Community
  • 1
  • 1
jamieweb
  • 123
  • 4

2 Answers2

1

First of all the json data in question is not an array but an object!

A simple way to visualise any piece of json data is to do this

<?php
$j = '{"time":{"updated":"Oct 26, 2015 12:54:00 UTC","updatedISO":"2015-10-26T12:54:00+00:00","updateduk":"Oct 26, 2015 at 12:54 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":"283.9203","description":"United States Dollar","rate_float":283.9203},"GBP":{"code":"GBP","rate":"185.0732","description":"British Pound Sterling","rate_float":185.0732}}}';

print_r( json_decode($j) );

Which in this case will produce :-

stdClass Object
(
    [time] => stdClass Object
        (
            [updated] => Oct 26, 2015 12:54:00 UTC
            [updatedISO] => 2015-10-26T12:54:00+00:00
            [updateduk] => Oct 26, 2015 at 12:54 GMT
        )

    [disclaimer] => This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchang
erates.org
    [bpi] => stdClass Object
        (
            [USD] => stdClass Object
                (
                    [code] => USD
                    [rate] => 283.9203
                    [description] => United States Dollar
                    [rate_float] => 283.9203
                )

            [GBP] => stdClass Object
                (
                    [code] => GBP
                    [rate] => 185.0732
                    [description] => British Pound Sterling
                    [rate_float] => 185.0732
                )

        )

)

This quite simply tells you a) its an object with properties that are also objects etc etc and b) how to reference any property within the object.

So to get the exchange rate you want I would do :-

<?php
$j = '{"time":{"updated":"Oct 26, 2015 12:54:00 UTC","updatedISO":"2015-10-26T12:54:00+00:00","updateduk":"Oct 26, 2015 at 12:54 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":"283.9203","description":"United States Dollar","rate_float":283.9203},"GBP":{"code":"GBP","rate":"185.0732","description":"British Pound Sterling","rate_float":185.0732}}}';

$bitExchange = json_decode($j);

// the GBP exchange rate will be
echo $bitExchange->bpi->GBP->rate;
// or if you prefer
echo $bitExchange->bpi->GBP->rate_float;
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

Assuming the json data is stored in json_text in python, you can do

import json
json.loads(json_text).get('bpi').get('USD').get('rate')

If the data is critical for you, you should check for errors. Also, making the currency configurable might be a good idea.

emre.
  • 1,296
  • 11
  • 13