nexus66 nexus66 - 1 month ago 18
JSON Question

Decoding a complex JSON correctly using Golang

I'm trying to parse a JSON from a URL. But I'm facing a problem while decoding values. Some are correct and the others are nil (empty strings or nil values for float64).

The JSON fetched from the URL is like this:

{
"status":"success",
"data":{
"coin":{
"name":"Bitcoin",
"abbr":"BTC",
"logo":"",
"homepage":""
},
"volume":{
"current":15967300,
"all":21000000,
"perc":76.03
},
"markets":{
"btce":{
"name":"BTC-e",
"last_update_utc":"2016-11-05T01:25:02Z",
"value":694.696,
"currency":"USD",
"daily_change":{
"value":"699.05600000",
"perc":-0.62,
"diff":-4.36
}
},
"coinbase":{
"name":"Coinbase",
"last_update_utc":"2016-11-05T01:25:02Z",
"value":705.65,
"currency":"USD",
"daily_change":{
"value":"700.87000000",
"perc":0.68000000000001,
"diff":4.78
}
}
},
"last_block":{
"nb":437388,
"time_utc":"2016-11-05T01:27:32Z",
"nb_txs":570,
"fee":"0.06545766",
"difficulty":"253618246641.490000000000000"
},
"next_difficulty":{
"difficulty":255675205724.12,
"retarget_in":84,
"retarget_block":437472,
"perc":0.81104538410295
},
"websocket":{
"ws_url":"ws:\/\/btc.blockr.io:9081",
"wss_url":"wss:\/\/btc.blockr.io:8081"
}
},
"code":200,
"message":""
}


My Go structs are:

type ResponseInfo struct {
Status string `json:"status"`
Data Info `json:"data"`
Code float64 `json:"code"`
Message string `json:"message,omitempty"`
}

type Info struct {
Coin _Coin `json:"coin"`
Volume _Volume `json:"volume"`
Markets _Markets `json:"markets"`
LastBlock _LastBlock `json:"last_block"`
NextDiff _NextDiff `json:"next_difficulty"`
WebSocket _WebSocket `json:"websocket"`
}

type _Coin struct {
Name string `json:"name"`
Abbr string `json:"abbr"`
Logo string `json:"logo"`
HomePage string `json:"homepage,omitempty"`
}

type _Volume struct {
Current float64 `json:"current"`
All float64 `json:"all"`
Perc float64 `json:"perc"`
}

type _Markets struct {
Btce _BtceInfo `json:"btce"`
Coinbase _CoinbaseInfo `json:"coinbase"`
}

type _BtceInfo struct {
Name string `json:"name"`
LastUpdate string `json:"last_update_utc"`
Value float64 `json:"value"`
Currency string `json:"currency"`
DailyChange _BtceDaily `json:"daily_change,omitempty"`
}

type _BtceDaily struct {
Value string `json:"value"`
Prec float64 `json:"prec"`
Diff float64 `json:"diff"`
}

type _CoinbaseInfo struct {
Name string `json:"name"`
LastUpdate string `json:"last_update_utc"`
Value float64 `json:"value"`
Currency string `json:"currency"`
DailyChange _CoinbaseDaily `json:"daily_change"`
}

type _CoinbaseDaily struct {
Value string `json:"value"`
Prec float64 `json:"prec"`
Diff float64 `json:"diff"`
}

type _LastBlock struct {
Nb float64 `json:"nb"`
Time string `json:"time_utc"`
NbTxs float64 `json:"nb_txs"`
Fee string `json:"fee"`
Difficulty string `json:"difficulty"`
}

type _NextDiff struct {
Difficulty float64 `json:"difficulty"`
RetargetIn float64 `json:"retarget_in"`
RetargetBlock float64 `json:"retarget_block"`
Perc float64 `json:"perc"`
}

type _WebSocket struct {
Wsurl string `json:"ws_url"`
WssUrl string `json:"wss_url"`
}


My incorrect output is:

{success {{Bitcoin BTC } {1.59675375e+07 2.1e+07 76.04} {{BTC-e 694 USD { 0 0}} {Coinbase 704.2 USD { 0 0}}} {0 0 } {0 0 0 0} { }} 200 }


Edit1:
Sorry the ouput my change because i'm parsing directly from the URL (http://btc.blockr.io/api/v1/coin/info)

Edit2:
Thanks to @John S Perayil who found the mistake.
My JSON Tags need to be
json:"<name>"
not
json="<name>"


Thanks.

Answer

All your json tags need to be json:"<name>" not json="<name>"