invalid character '<' looking for beginning of value

ErrorGo

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.

broken.jsonFails to parse
<!DOCTYPE html>
<html><body>404 page not found</body></html>
JSON.parse stops at line 1, column 1.

Lines 1, 2, 3 and 4 change.

fixed.jsonParses
{
  "error": "not found",
  "status": 404
}
JSON.parse reads it.

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.

Console
  • 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

  1. 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.

  2. '}' 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.

  3. 'N' or 'o': a keyword that is not quite null

    None from Python and a capital NULL both start with a capital N, so Go stops on that first byte and says invalid character 'N' looking for beginning of value. A lowercase word that starts like null, such as none, gets one letter further: invalid character 'o' in literal null (expecting 'u'). A null cut short by a truncated response is a third case. Go has run out of input, so it says unexpected end of JSON input.

  4. An empty body

    Reported separately as unexpected end of JSON input. Usually a 204, a HEAD request, or a body that was already read once: an http.Response.Body can 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.

What you put into the tools never leaves your browser. We count page views and keep them for 90 days. Accept and we also remember this browser, so a return visit is not counted as new. Privacy policy