I am new to GO and I am trying to consume json data from a variety of API's using GO and place them in struct's one of them Formats the data like so
{"MS": {
"last":"25",
"highestBid":"20"},
"GE": {
"last": "24",
"highestBid": "22"}
}
While I can find information on Consuming with dynamic keys, all the examples I found throw away the Outer most key as arbitrary and irrelevant. I need to use it as a key value pair like bellow:
{"MarketName": "GE", "last":"24","higestBid":"22"}
I understand Using Interface map but I cant figure out how to map the dynamic key to the struct as a key : value pair like above. My Code example to map leaving out the need key can be found at play ground Relevant Code
package main
import (
"encoding/json"
"fmt"
)
var jsonBytes = []byte(`
{"MS": {
"last":"25",
"highestBid":"20"},
"GE": {
"last": "24",
"highestBid": "22"}
}`)
type Market struct {
Last string
HighestBid string
}
func main() {
// Unmarshal using a generic interface
var f interface{}
err := json.Unmarshal(jsonBytes, &f)
if err != nil {
fmt.Println("Error parsing JSON: ", err)
}
fmt.Println(f)
}
as it stands it outputs
map[MS:map[last:25 highestBid:20] GE:map[highestBid:22 last:24]]
As I stated I am new to GO and as much help and explanation that i can get would be very appreciated