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
I believe that in the JSON, the 'now' entry (final line) is the current exchange rate. – George Pearce – 2013-05-04T23:35:36.290
can't be, it has this huge value: 1367714938558901 – knocte – 2013-05-05T00:53:12.413
Sorry, I meant the line after the identifier "last" - first line is last_local, second is last, etc etc. – George Pearce – 2013-05-05T09:18:14.207
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 – 2013-05-05T12:29:43.090
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 – 2013-05-05T13:32:26.463
1now is the current time, to the nano sec. precision. (1367714938 - sec. since 1970; 558 - micro sec.; 901 - nano sec.) – vi.su. – 2013-05-06T06:40:22.950