The user with no hat
The user with no hat

Reputation: 10846

Can I decode JSON only partially

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.

Upvotes: 1

Views: 3282

Answers (1)

Nick Craig-Wood
Nick Craig-Wood

Reputation: 54079

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)
}

Upvotes: 11

Related Questions