2

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. 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, 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?

  • I believe that in the JSON, the 'now' entry (final line) is the current exchange rate. – George Pearce May 4 '13 at 23:35
  • can't be, it has this huge value: 1367714938558901 – knocte May 5 '13 at 0:53
  • Sorry, I meant the line after the identifier "last" - first line is last_local, second is last, etc etc. – George Pearce May 5 '13 at 9:18
  • 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 May 5 '13 at 12:29
  • 1
    now is the current time, to the nano sec. precision. (1367714938 - sec. since 1970; 558 - micro sec.; 901 - nano sec.) – vi.su. May 6 '13 at 6:40
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
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

-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://bitkapp.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.

  • 1
    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 May 6 '13 at 20:24
  • 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 May 7 '13 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.