I've following json
[{"href":"/publication/192a7f47-84c1-445e-a615-ff82d92e2eaa/article/b;version=1493756861347"},{"href":"/publication/192a7f47-84c1-445e-a615-ff82d92e2eaa/article/a;version=1493756856398"}]
Based on the given answer I've tried following
var objmap map[string]*json.RawMessage
err := json.Unmarshal(data, &objmap)
I'm getting empty array with following error. any suggestions?
json: cannot unmarshal array into Go value of type map[string]*json.RawMessage
You can unmarshall to a []map[string]interface{}
as follows:
data := []byte(`[{"href":"/publication/192a7f47-84c1-445e-a615-ff82d92e2eaa/article/b;version=1493756861347"},{"href":"/publication/192a7f47-84c1-445e-a615-ff82d92e2eaa/article/a;version=1493756856398"}]`)
var objmap []map[string]interface{}
if err := json.Unmarshal(data, &objmap); err != nil {
log.Fatal(err)
}
fmt.Println(objmap[0]["href"]) // to parse out your value
To see more on how unmarshalling works see here: https://godoc.org/encoding/json#Unmarshal
This is not direct answer but I think very useful
Json Unmarshal & Indent without struct
func JsonIndent (jsontext []byte) ([]byte,error) {
var err error
var jsonIndent []byte
var objmap map[string]*json.RawMessage
err = json.Unmarshal(jsontext, &objmap)
if err != nil {
return jsonIndent,err
}
jsonIndent, err = json.MarshalIndent(objmap,"", " ")
return jsonIndent,err
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With