I mainly program in python and am in the process of teaching myself some Go!
I've come to a stumbling block in trying to parse JSON data from an api call and I am turning to the great community here for some advice! Nothing I can find online has worked yet so any advice, even if its just related to the rest of the code, would be helpful!
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
//Time blah
type Time struct {
Updated string `json:"updated"`
UpdatedISO string `json:"updatedISO"`
Updateduk string `json:"updateduk"`
}
//Currency blah blah
type Currency struct {
Code string `json:"code"`
Rate float64 `json:"rate"`
Descr string `json:"description"`
Rf float64 `json:"rate-float"`
}
//Bpi blah
type Bpi struct {
Usd []Currency `json:"USD"`
Eur []Currency `json:"EUR"`
}
//ListResponse is a blah
type ListResponse struct {
Time []Time `json:"time"`
Disclaimer string `json:"disclaimer"`
Bpi []Bpi `json:"bpi"`
}
func btcPrice() {
url := "https://api.coindesk.com/v1/bpi/currentprice/eur.json"
req, err := http.Get(url)
if err != nil {
panic(err.Error())
} else {
data, err := ioutil.ReadAll(req.Body)
if err != nil {
fmt.Println("Error at the data stage: ", err)
}
// fmt.Println(string(data))
var result ListResponse
json.Unmarshal(data, &result)
fmt.Println(result)
}
}
func main() {
btcPrice()
}
Basically the code above is to make an api call, take the JSON data and manipulate it using my structs above. (I may be going at this the wrong way altogether so if that's the case call me for it).
Here is where things go belly up for me. The JSON returned is the following:
{
"time":{
"updated":"Dec 10, 2017 20:41:00 UTC",
"updatedISO":"2017-12-10T20:41:00+00:00",
"updateduk":"Dec 10, 2017 at 20:41 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":"15,453.3888",
"description":"United States Dollar",
"rate_float":15453.3888
},
"EUR":{
"code":"EUR",
"rate":"13,134.4532",
"description":"Euro",
"rate_float":13134.4532
}
}
}
What I want to be able to do is access the json data sets individually, but the issue appears to be in my structs. If I call a basic print (commented in go code above for you to see), the whole data set from the json prints.But when I use the result code and json.unmarshal it only gives me the disclaimer, i.e the part of the struct simply labelled as of type string.
I may seem to be rambling and will clarify statements if they are questioned in the comments.
To Summarise:
- The code above fetches JSON and attempts to decode it using my structs created above.
- The data itself is fine, I've tested that with a simple print function.
- The issue could be in the structs (more than likely).
- What I want to be able to do with this code is basically access the "rate_float" amount in the "EUR" data subset of the "bpi" data set of the JSON file. (Taking the euro amount and using it for a slack bot as a small project).
[]from the relevant fields. (example play.golang.org/p/9dT8TShrea)rate_floatwhile your tag in the go struct saysrate-float, that won't match and therefore will not be unmarshaled as you might expect.