Question

I have a quite complex json document but I need to decode only one string locationx. I'm wondering if it's possible to decode only a specific field (matching by name somehow ) without to write the struct for the whole document. I've seen that sometimes it works to decode json documents even if the struct doesn't match 100% the document structure.

Was it helpful?

Solution

Yes, you can just mention the fields you are interested in and the decoder will ignore any others, eg

type MyData struct {
    Location  string `json:"locationx"`
}
var x MyData
err := json.Unmarshal(jsonBlob, &x)
if err != nil {
    fmt.Println("error:", err)
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top