go data_error ai_generated true

invalid character '\x00' looking for beginning of value

ID: go/json-invalid-character-looking-for-beginning

Also available as: JSON · Markdown · 中文
80%Fix Rate
83%Confidence
0Evidence
2024-07-03First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active — — —
1.22 active — — —

Root Cause

The input to the JSON decoder begins with a NUL byte or non-JSON garbage, e.g. binary content, BOM, or an empty gzip stream.

generic

中文

JSON 解码器输入以 NUL 字节或非 JSON 垃圾开头,例如二进制内容、BOM 或空 gzip 流。

Workarounds

  1. 85% success
    b = bytes.TrimPrefix(b, []byte{0xEF,0xBB,0xBF})
    b = bytes.Trim(b, "\x00")
    if !json.Valid(b) { return fmt.Errorf("invalid json: %q", b[:min(32,len(b))]) }
  2. 90% success
    Log the first 64 bytes of the body before decoding to diagnose the actual content.

Dead Ends

Common approaches that don't work:

  1. 80% fail

    Does not remove NUL bytes or BOM.

  2. 60% fail

    Returns false but gives no insight into the cause.