invalid character '<' looking for beginning of value
invalid character '<' looking for beginning of value
Where JSON.parse stops
<!DOCTYPE html>
<html><body>404 page not found</body></html>What it means
Go tells you two things: the byte it could not use, and what it was in the middle of reading. "looking for beginning of value" means it was at the start of a value, so the byte quoted is the very first character of something that was supposed to be JSON (which is why < is the one people see most).
Last verified on Go 1.27.
It opens with the error marked. Nothing is uploaded. Find where the error is.
- Usually
- The byte is '<', so you are unmarshalling HTML'}' looking for beginning of object key string'N' or 'o': a keyword that is not quite nullAn empty body
- Fix
- Check the status code and content type before you unmarshal, and log the raw body when they are not what you expect.
The fix
Check the status code and content type before you unmarshal, and log the raw body when they are not what you expect. The two samples here are the same failed request: what arrived, and what the API should have returned. The fix is upstream, not in the parsing code.
<!DOCTYPE html>
<html><body>404 page not found</body></html>Lines 1, 2, 3 and 4 change.
{
"error": "not found",
"status": 404
}How the message appears
Go wording, from encoding/json. The error is a *json.SyntaxError and carries a byte Offset field that the string form does not show.
- invalid character '<' looking for beginning of value
- invalid character '}' looking for beginning of object key string
- invalid character 'N' looking for beginning of value
- invalid character 'o' in literal null (expecting 'u')
- unexpected end of JSON input
What causes it
The byte is '<', so you are unmarshalling HTML
A 404 page, a login redirect, a proxy or load-balancer error, or a WAF challenge. This is by far the most common form of the message, and no change to your struct or your JSON will fix it, because there is no JSON involved.
'}' looking for beginning of object key string
A trailing comma. The parser consumed the comma, went looking for the next key, and found the closing brace instead.
'N' or 'o': a keyword that is not quite null
Nonefrom Python and a capitalNULLboth start with a capital N, so Go stops on that first byte and saysinvalid character 'N' looking for beginning of value. A lowercase word that starts likenull, such asnone, gets one letter further:invalid character 'o' in literal null (expecting 'u'). Anullcut short by a truncated response is a third case. Go has run out of input, so it saysunexpected end of JSON input.An empty body
Reported separately as
unexpected end of JSON input. Usually a204, aHEADrequest, or a body that was already read once: anhttp.Response.Bodycan only be consumed a single time.
Worth knowing
Watch outDecoding into interface{} never produces that type error and is a poor workaround: every number becomes a float64, which quietly mangles large integer IDs.
NoteThe offset is available programmatically: var se *json.SyntaxError; if errors.As(err, &se) { log.Print(se.Offset) } gives the byte position, which beats guessing from the character alone.
NoteA different family of Go errors means the opposite of this one. When the API sends {"n": "12"} and your struct has N int tagged n, Go says json: cannot unmarshal string into Go struct field payload.n of type int, with your own type and field in place of payload.n. The document is valid JSON and only the target type is wrong. There is nothing to fix in the JSON. Change the field type, or give it a json.Number or a custom UnmarshalJSON.