Decoding JSON Responses
Parsing API responses into Go structs
Decoding JSON Responses is a free Go Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Check status before decoding
Always verify the response status is 2xx before attempting to decode the body. Non-2xx responses may contain HTML error pages, not JSON.
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("status %d: %s", resp.StatusCode, body)
}json.NewDecoder
Stream-decode the response body without loading it fully into memory:
var result APIResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return fmt.Errorf("decode: %w", err)
}Decoding into a generic map
For unknown or dynamic JSON shapes, decode into map[string]any:
var m map[string]any
json.NewDecoder(resp.Body).Decode(&m)Nested structs
Model nested JSON with nested Go structs and json tags:
type Repo struct {
Name string `json:"name"`
Owner struct {
Login string `json:"login"`
} `json:"owner"`
}Optional fields with pointers
Use pointer types for optional JSON fields so you can distinguish "field absent" from "field set to zero value":
type Response struct {
Data *string `json:"data"`
Error *string `json:"error"`
}Decoding arrays
Decode JSON arrays directly into Go slices:
var users []User
json.NewDecoder(resp.Body).Decode(&users)Checking for decode errors
Distinguish JSON syntax errors from unexpected EOF errors — EOF means the body is empty (no JSON at all).
err := json.NewDecoder(resp.Body).Decode(&v)
if errors.Is(err, io.EOF) {
return fmt.Errorf("empty response body")
}Content-Type check
Verify the response Content-Type is application/json before decoding to avoid silently parsing HTML as JSON.
ct := resp.Header.Get("Content-Type")
if !strings.HasPrefix(ct, "application/json") {
return fmt.Errorf("unexpected content-type: %s", ct)
}Ignoring unknown fields
json.Decoder silently ignores JSON fields that have no matching struct field. This is the default and usually desirable for forward compatibility.
Strict decoding with DisallowUnknownFields
Call dec.DisallowUnknownFields() to return an error for unknown keys — useful for strict config parsing but not for consuming third-party APIs.
dec := json.NewDecoder(r.Body)
dec.DisallowUnknownFields()
dec.Decode(&cfg)Reusing the decoded struct
Zero the struct before decoding if reusing across multiple responses to avoid stale data from a previous decode.
var result APIResult
result = APIResult{} // reset
json.NewDecoder(resp.Body).Decode(&result)Quick Check
What error does json.Decoder.Decode return when the response body is empty?
Recap: Decoding JSON Responses
Key points:
- Check status code before decoding
- json.NewDecoder for streaming; avoids full body in memory
- Verify Content-Type is application/json
- io.EOF means empty body; io.ErrUnexpectedEOF means truncated JSON
Frequently asked questions
Is the “Decoding JSON Responses” lesson free?
Yes — the full text of “Decoding JSON Responses” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Decoding JSON Responses”?
Parsing API responses into Go structs You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Decoding JSON Responses” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Making HTTP Requests
- Setting Timeouts and Headers
- Decoding JSON Responses
- Error Handling and Retry Logic