Debugging a JSON error from Golang
<p>I'm fetching and decoding a large JSON response that has an error in it. Now I need to find <strong>where</strong> the error is! I <a href="https://blog.golang.org/error-handling-and-go" rel="noreferrer">read about json.SyntaxError</a> but I am struggling to find out how to use it.</p>

<pre><code>package main

import (
    "encoding/json"
    "fmt"
    "net/http"
    "os"
    "text/template"
    "time"
)

type Movie struct {
    Title       string    `json:"title"`
    PublishedAt time.Time `json:"published_at"`
}

func main() {
    req, _ := http.NewRequest("GET", "https://s.natalian.org/2016-12-07/debugme2.json", nil)
    resp, err := http.DefaultClient.Do(req)

    defer resp.Body.Close()
    dec := json.NewDecoder(resp.Body)

    _, err = dec.Token()
    for dec.More() {
        var m Movie
        if err = dec.Decode(&amp;m); err != nil {
            fmt.Println(err)
            fmt.Println("Bad", m)

            // https://blog.golang.org/error-handling-and-go
            if serr, ok := err.(*json.SyntaxError); ok {
                fmt.Println("Syntax error", serr)
            }

        } else {
            fmt.Println("Good", m)
        }

        tmpl := template.Must(template.New("test").Parse("OUTPUT: {{ if .Title }}{{.Title}}{{ if .PublishedAt }} was published at {{.PublishedAt}} {{ end }}{{end}}\n"))
        tmpl.Execute(os.Stdout, m)
    }

}
</code></pre>

<p>What am I missing? Any tools or strategies or suggestions would be much appreciated. My output currently looks like:</p>

<pre><code>Good {foobar 2016-11-24 16:17:12 +0800 SGT}
OUTPUT: foobar was published at 2016-11-24 16:17:12 +0800 SGT
parsing time ""null"" as ""2006-01-02T15:04:05Z07:00"": cannot parse "null"" as "2006"
Bad {barbar 0001-01-01 00:00:00 +0000 UTC}
OUTPUT: barbar was published at 0001-01-01 00:00:00 +0000 UTC
Good { 1999-12-24 16:11:12 +0200 +0200}
OUTPUT:
Good {Something else entirely 2000-01-24 16:11:12 +0200 +0200}
OUTPUT: Something else entirely was published at 2000-01-24 16:11:12 +0200 +0200
</code></pre>

<p>But I need something like this in my stderr to <strong>better debug the issue</strong>:</p>

<pre><code>Line 8: published_at is invalid
</code></pre>

<p>And maybe some context of the Title so I can tell the API backend team they have an error in their JSON response.</p>

<p><strong>BONUS question</strong>: Furthermore I don't want to print the value <em>0001-01-01 00:00:00 +0000 UTC</em> as it's actually really empty. I don't actually mind it being missing.</p>