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
Previous
Why is omitempty not working for my Go struct fields?
Next
How can I ignore specific fields during JSON marshaling in Go?
Related Questions
How do I use Go struct tags to rename JSON fields?
Learn how to use Go struct tags to rename JSON fields. Master JSON marshaling with proper field naming and omitempty.
Why is omitempty not working for my Go struct fields?
Learn why omitempty is not working in Go struct JSON tags. Understand zero values, pointers, and proper optional field handling.
How can I ignore specific fields during JSON marshaling in Go?
Learn how to ignore fields during JSON marshaling in Go. Master struct tags, custom marshaling, and data hiding techniques.
Still have questions?
Can't find the answer you're looking for? Please reach out to our support team.