package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
)
type Data struct {
CatalogID string `json:"catalogId"`
ProductID string `json:"productId"`
Keywords string `json:"keywords"`
Price string `json:"price"`
PriceSale string `json:"price_sale"`
PercentOff string `json:"percentOff"`
Currency string `json:"currency"`
Upc string `json:"upc"`
Isbn string `json:"isbn"`
Sales int `json:"sales"`
}
type DataPage struct {
D []Data `json:"data"`
}
func main() {
// Read in the json file.
bytes, err := ioutil.ReadFile("file.json")
if err != nil {
log.Fatalln(err)
}
var datapage DataPage
if err := json.Unmarshal(bytes, &datapage); err != nil {
panic(err)
}
fmt.Println(datapage)
}
In the above example, I put the JSON in a file called file.json to make things simpler.
Just one other thing. For the Sales field, since you used an integer in the JSON and not a string, it needed to be an switched to an int field instead of a string field to work.
Edit: You could also have used this instead of using 2 separate structures:
type DataPage struct {
D []struct {
CatalogID string `json:"catalogId"`
ProductID string `json:"productId"`
Keywords string `json:"keywords"`
Price string `json:"price"`
PriceSale string `json:"price_sale"`
PercentOff string `json:"percentOff"`
Currency string `json:"currency"`
Upc string `json:"upc"`
Isbn string `json:"isbn"`
Sales int `json:"sales"`
} `json:"data"`
}