Go JSON

When should I use json.NewDecoder vs json.Unmarshal in Go?

Use json.Unmarshal() for complete JSON data already in memory as []byte or string. Use json.NewDecoder() when reading from io.Reader streams like HTTP response bodies, files, or network connections. NewDecoder is more efficient for streaming data as it reads and parses incrementally without loading everything into memory first. For single JSON objects in memory, Unmarshal is simpler: json.Unmarshal(data, &v). For HTTP responses, use decoder directly: json.NewDecoder(resp.Body).Decode(&v) avoids unnecessary buffering. Decoder supports multiple JSON values in sequence with repeated Decode() calls. For large files, decoder conserves memory. Unmarshal requires complete data upfront. Decoder works with any io.Reader making it more flexible. Use our JSON Editor at jsonconsole.com/json-editor to prepare test JSON for both approaches. Choose based on data source: Unmarshal for complete data in memory, NewDecoder for streams and readers. Both are type-safe and equally reliable.
Last updated: December 23, 2025

Still have questions?

Can't find the answer you're looking for? Please reach out to our support team.