how to find, "invalid character ',' looking for beginning of value" error message

First, as has been commented, are you sure you can't use the go/build package directly rather than running go list?

I Wouldn't use println (or fmt.Println) inside HTTP handlers. It's much better to use log.Println and/or get the error into the ResponseWriter. Also, it's a good idea to wrap your ListenAndServe call with log.Fatal.

When printing/logging error values you can just use err, no need to have err.Error().

Further, when you actually want to do something more detailed than just reporting/logging the error message you can look at it's type and other info. For example, log.Printf("verbose error info: %#v", err) gives:

&json.SyntaxError{msg:"invalid character ',' looking for beginning of value", Offset:0}

I tried this because I know the json package returns various error types with additional info and I was hoping the offset value would be of help. If it had been then something like this might have been helpful:

if err := json.Compact(…) {
    if err != nil {
        log.Println("json.Compact:", err)
        if serr, ok := err.(*json.SyntaxError); ok {
            log.Println("Occurred at offset:", serr.Offset)
            // … something to show the data in buff around that offset …
        }
    }
}

But offset zero isn't helpful :(

So although this doesn't identify you problem hopefully it can be of some help to your further investigation.

Edit:

So after adding:

log.Println("Write file:", ioutil.WriteFile("data.json", buff.Bytes(), 0600))

to the above error handling block I then ran a JSON validator on the resultant file and it identified this piece:

        "XTestImports": [
                "io",
                "log",
                "net"
        ]
},,{
        "Dir": "/usr/local/go/src/mime",
        "ImportPath": "mime",
        "Name": "mime",

Note the double ,,.

That should tell you whete the error in your code is. But if not, you need to skip empty entries, either when processing t.J or when you build it. The later is better and just involves:

    if len(newj) > 0 {
        myObj.J = append(myObj.J, newj)
    }

(where btw you don't check for errors from json.Unmarshal so it's not clear if that is supposed to ever be empty or if it's empty due to a preceeding error. Never ignore error returns!)


I also encountered the same error message in a Go program, but the error message was within the HTTP response error, in HTML format when my HTTP response parser expected JSON.

For me, the solution was to change my request to include setting the Content-Type header to application/json. How you do this depends on which http client library you happen to be using; if you have access to the http.Header core type, you can set the header with .Set(...).

I realize the scope of this fix for me may not apply to the original question, but I came here first after googling and thought this would help others, since the message was not particularly obvious at first glance. The hint is that the invalid < character is the first HTML character in the error/response, which is likely the result of the request type not being set to application/json, thus the server responds with a non JSON response.


For me the issue was I was trying to parse the already parsed JSON.

Tags:

Go