<p>JSON can be a little hard to read because it's generally meant to be served without characters such as spaces and newlines that would make it human-readable.</p>

<p>Here's output from the fast ticker:</p>

<pre><code>{
  "result":"success",
  "data":{
    "last_local":{
      "value":"136.87303",
      "value_int":"13687303",
      "display":"$136.87",
      "display_short":"$136.87",
      "currency":"USD"
    },
    "last":{
      "value":"136.87303",
      "value_int":"13687303",
      "display":"$136.87",
      "display_short":"$136.87",
      "currency":"USD"
    },
    "last_orig":{
      "value":"136.87303",
      "value_int":"13687303",
      "display":"$136.87",
      "display_short":"$136.87",
      "currency":"USD"
    },
    "last_all":{
      "value":"136.87303",
      "value_int":"13687303",
      "display":"$136.87",
      "display_short":"$136.87",
      "currency":"USD"
    },
    "buy":{
      "value":"136.10500",
      "value_int":"13610500",
      "display":"$136.11",
      "display_short":"$136.11",
      "currency":"USD"
    },
    "sell":{
      "value":"136.87250",
      "value_int":"13687250",
      "display":"$136.87",
      "display_short":"$136.87",
      "currency":"USD"
    },
    "now":"1381085718504609"
  }
}
</code></pre>

<p>The "result" item is always guaranteed to be there, but has of course varying content based on whether or not the call was successful. The "data" element contains a series of prices, with each name indicating its price.</p>

<p>Most JSON parsers in interpreted languages such as Ruby, PHP, and Python parse JSON into a hash or dictionary. You'd want the value of the "value" key of the "last" key of the "data" key.</p>

<p>Here's a one-liner version in Ruby for the v2 ticker:</p>

<pre><code>ruby -e '%w(open-uri json).each{|b| require b}; open("http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast") {|d| puts JSON.parse(d.read)["data"]["last"]["value"]}'
</code></pre>

<p>Or something a little more readable:</p>

<pre><code>require "open-uri" 
require "json"

open("http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast") do |d| 
  json = JSON.parse(d.read)
  puts json["data"]["last"]["value"]
end
</code></pre>

<p>Here's a one-liner version in Ruby for the old ticker:</p>

<pre><code>ruby -e '%w(open-uri json).each{|b| require b}; open("https://mtgox.com/api/1/BTCUSD/ticker") {|d| puts JSON.parse(d.read)["return"]["last"]["value"]}'
</code></pre>

<p>Or something a little more readable:</p>

<pre><code>require "open-uri" 
require "json"

open("https://mtgox.com/api/1/BTCUSD/ticker") do |d| 
  json = JSON.parse(d.read)
  puts json["return"]["last"]["value"]
end
</code></pre>

CC BY-SA 3.0
2013-10-06T18:52:16.487Z
2013-10-06T19:01:34.623Z
2013-10-06T19:01:34.623Z
Colin Dean
<p>Ok this should answer your question,</p>

<pre><code>import urllib2, json

x = float(json.loads(urllib2.urlopen('https://mtgox.com/api/1/BTCUSD/ticker').read())['return']['last']['value'])

print x
</code></pre>

<p>This returns the last value for the MTGox USD price for bitcoins</p>

CC BY-SA 3.0
2013-05-09T16:29:37.623Z
2013-05-09T16:29:37.623Z
bitkapp
<p>This here is a bitcoin ticker app it shows the MTGox bitcoin price with a small lag and is self refreshing. It also supports GBP, EUR and Yen. This is the link to the blog post <a href="http://bitkapp.com/blog/bitcoin-price-ticker/" rel="nofollow">http://bitkapp.com/blog/bitcoin-price-ticker/</a> </p>

<p>The simplest way of receiving the bitcoin exchange rate without a self refreshing feature is done by the following python script.</p>

<pre><code>import json
import urllib2
from Tkinter import *

url='http://api.bitcoincharts.com/v1/markets.json'
req=urllib2.Request(url)
response=urllib2.urlopen(req).read()
output=json.loads(response)

mtusd_price = output[-10]['avg']
mteur_price = output[31]['avg']
mtgbp_price = output[44]['avg']

MTUSD= 'USD' + ' ' + str(mtusd_price)
MTEUR= 'EUR' + ' ' + str(mteur_price)
MTGBP = 'GBP' + ' ' + str(mtgbp_price)

root= Tk()
root.title("Bitcoin Price")
root.geometry("250x100")

USD = Label(root, text=MTUSD)
EUR = Label(root, text=MTEUR)
GBP = Label(root, text=MTGBP)
USD.pack()
EUR.pack()
GBP.pack()
mainloop()
</code></pre>

<p>When you run this script it will open a window displaying the price at that instant in USD, EUR and GBP.</p>

<p>Im sorry for previous post as it was obviously not clear enough.</p>

CC BY-SA 3.0
2013-05-06T20:24:48.790Z
WTF! This not only uses the average price (instead of the latest trade), but also uses numbers to locate the elements in the json!! the most horrible code I've ever seen in a very big while, downvoting
knocte
CC BY-SA 3.0
2013-05-07T08:51:12.890Z
I suppose you could define a function that searches the json output. Dont hate though man im new to all this programming thing. Did you have a look at the ticker app though?
bitkapp
CC BY-SA 3.0
2013-05-06T11:55:47.843Z
2013-05-06T11:55:47.843Z
bitkapp
<p>I want to do a simple app that refreshes every minute to tell me the current exchange rate by Mtgox but I have a problem interpreting the JSON that some APIs return:</p>

<ul>
<li><p>Mtgox's fast ticker returns <a href="http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast" rel="nofollow">this JSON</a>. The question is, what's the value that I have to retrieve? There are a lot and I don't understand them, and I don't find proper docs.</p></li>
<li><p>Bitcoincharts' API has the <a href="http://bitcoincharts.com/about/markets-api/" rel="nofollow">Markets Data endpoint</a>, and it returns the market I want under the symbol "mtgoxUSD", however it tells me the date time of latest trade, the last buy/ask, but doesn't tell me <strong>the exchange rate of the latest trade</strong>.</p></li>
</ul>

<p>Am I missing something?</p>

CC BY-SA 3.0
2013-05-04T23:35:36.290Z
I believe that in the JSON, the 'now' entry (final line) is the current exchange rate.
George Pearce
CC BY-SA 3.0
2013-05-05T00:53:12.413Z
can't be, it has this huge value: 1367714938558901
knocte
CC BY-SA 3.0
2013-05-05T09:18:14.207Z
Sorry, I meant the line after the identifier "last" - first line is last_local, second is last, etc etc.
George Pearce
CC BY-SA 3.0
2013-05-05T12:29:43.090Z
Mmm, you may be right, but I've been refreshing the page in intervals of about 10 seconds, and the value doesn't change!!! I'm wondering if it's because I'm not using a registered key?
knocte
CC BY-SA 3.0
2013-05-05T13:32:26.463Z
I have the btcReport iOS app which uses that API and it only updates once per 60 seconds. I just checked it and it agrees with the value I have for the exchange rate at the moment.
George Pearce
CC BY-SA 3.0
2013-05-06T06:40:22.950Z
now is the current time, to the nano sec. precision. (1367714938 - sec. since 1970; 558 - micro sec.; 901 - nano sec.)
vi.su.
<mtgox><json-rpc><api><exchange-rate>
Simplest way of obtaining MtGox exchange rate via an API?
knocte
# Simplest way of obtaining MtGox exchange rate via an API?

(Asked by: username_0 on 2013-05-04)

I want to do a simple app that refreshes every minute to tell me the current exchange rate by Mtgox but I have a problem interpreting the JSON that some APIs return:

* Mtgox's fast ticker returns [this JSON](http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast). The question is, what's the value that I have to retrieve? There are a lot and I don't understand them, and I don't find proper docs.
* Bitcoincharts' API has the [Markets Data endpoint](http://bitcoincharts.com/about/markets-api/), and it returns the market I want under the symbol "mtgoxUSD", however it tells me the date time of latest trade, the last buy/ask, but doesn't tell me **the exchange rate of the latest trade**.

Am I missing something?

**Tags:** mtgox, json-rpc, api, exchange-rate

> **Comments:**
> **username_1**: I believe that in the JSON, the 'now' entry (final line) is the current exchange rate.  
> **username_0**: can't be, it has this huge value: 1367714938558901  
> **username_1**: Sorry, I meant the line after the identifier "last" - first line is last_local, second is last, etc etc.  
> **username_0**: Mmm, you may be right, but I've been refreshing the page in intervals of about 10 seconds, and the value doesn't change!!! I'm wondering if it's because I'm not using a registered key?  
> **username_1**: I have the btcReport iOS app which uses that API and it only updates once per 60 seconds. I just checked it and it agrees with the value I have for the exchange rate at the moment.  
> **username_2**: now is the current time, to the nano sec. precision. (1367714938 - sec. since 1970; 558 - micro sec.; 901 - nano sec.)  

---

## 3 answers

### Answer by username_3 on 2013-10-06. Score: 2

JSON can be a little hard to read because it's generally meant to be served without characters such as spaces and newlines that would make it human-readable.

Here's output from the fast ticker:

```
{
  "result":"success",
  "data":{
    "last_local":{
      "value":"136.87303",
      "value_int":"13687303",
      "display":"$136.87",
      "display_short":"$136.87",
      "currency":"USD"
    },
    "last":{
      "value":"136.87303",
      "value_int":"13687303",
      "display":"$136.87",
      "display_short":"$136.87",
      "currency":"USD"
    },
    "last_orig":{
      "value":"136.87303",
      "value_int":"13687303",
      "display":"$136.87",
      "display_short":"$136.87",
      "currency":"USD"
    },
    "last_all":{
      "value":"136.87303",
      "value_int":"13687303",
      "display":"$136.87",
      "display_short":"$136.87",
      "currency":"USD"
    },
    "buy":{
      "value":"136.10500",
      "value_int":"13610500",
      "display":"$136.11",
      "display_short":"$136.11",
      "currency":"USD"
    },
    "sell":{
      "value":"136.87250",
      "value_int":"13687250",
      "display":"$136.87",
      "display_short":"$136.87",
      "currency":"USD"
    },
    "now":"1381085718504609"
  }
}

```

The "result" item is always guaranteed to be there, but has of course varying content based on whether or not the call was successful. The "data" element contains a series of prices, with each name indicating its price.

Most JSON parsers in interpreted languages such as Ruby, PHP, and Python parse JSON into a hash or dictionary. You'd want the value of the "value" key of the "last" key of the "data" key.

Here's a one-liner version in Ruby for the v2 ticker:

```
ruby -e '%w(open-uri json).each{|b| require b}; open("http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast") {|d| puts JSON.parse(d.read)["data"]["last"]["value"]}'

```

Or something a little more readable:

```
require "open-uri" 
require "json"

open("http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast") do |d| 
  json = JSON.parse(d.read)
  puts json["data"]["last"]["value"]
end

```

Here's a one-liner version in Ruby for the old ticker:

```
ruby -e '%w(open-uri json).each{|b| require b}; open("https://mtgox.com/api/1/BTCUSD/ticker") {|d| puts JSON.parse(d.read)["return"]["last"]["value"]}'

```

Or something a little more readable:

```
require "open-uri" 
require "json"

open("https://mtgox.com/api/1/BTCUSD/ticker") do |d| 
  json = JSON.parse(d.read)
  puts json["return"]["last"]["value"]
end

```


---

### Answer by username_4 on 2013-05-09. Score: 0

Ok this should answer your question,

```
import urllib2, json

x = float(json.loads(urllib2.urlopen('https://mtgox.com/api/1/BTCUSD/ticker').read())['return']['last']['value'])

print x

```

This returns the last value for the MTGox USD price for bitcoins


---

### Answer by username_4 on 2013-05-06. Score: -2

This here is a bitcoin ticker app it shows the MTGox bitcoin price with a small lag and is self refreshing. It also supports GBP, EUR and Yen. This is the link to the blog post <http://username_4.com/blog/bitcoin-price-ticker/>

The simplest way of receiving the bitcoin exchange rate without a self refreshing feature is done by the following python script.

```
import json
import urllib2
from Tkinter import *

url='http://api.bitcoincharts.com/v1/markets.json'
req=urllib2.Request(url)
response=urllib2.urlopen(req).read()
output=json.loads(response)

mtusd_price = output[-10]['avg']
mteur_price = output[31]['avg']
mtgbp_price = output[44]['avg']

MTUSD= 'USD' + ' ' + str(mtusd_price)
MTEUR= 'EUR' + ' ' + str(mteur_price)
MTGBP = 'GBP' + ' ' + str(mtgbp_price)

root= Tk()
root.title("Bitcoin Price")
root.geometry("250x100")

USD = Label(root, text=MTUSD)
EUR = Label(root, text=MTEUR)
GBP = Label(root, text=MTGBP)
USD.pack()
EUR.pack()
GBP.pack()
mainloop()

```

When you run this script it will open a window displaying the price at that instant in USD, EUR and GBP.

Im sorry for previous post as it was obviously not clear enough.

> **Comments:**
> **username_0**: WTF! This not only uses the average price (instead of the latest trade), but also uses numbers to locate the elements in the json!! the most horrible code I've ever seen in a very big while, downvoting  
> **username_4**: I suppose you could define a function that searches the json output. Dont hate though man im new to all this programming thing. Did you have a look at the ticker app though?  

---

