go data_error ai_generated true

error: invalid UTF-8 in string literal

ID: go/encoding-json-invalid-utf8-in-string

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.20 active
1.21 active

Root Cause

JSON input contains invalid UTF-8 byte sequences within a string, violating JSON specification.

generic

中文

JSON输入在字符串中包含无效的UTF-8字节序列,违反了JSON规范。

Workarounds

  1. 85% success Use json.Decoder with DisallowUnknownFields and validate input
    decoder := json.NewDecoder(reader)
    decoder.DisallowUnknownFields()
    if err := decoder.Decode(&target); err != nil { /* handle */ }

Dead Ends

Common approaches that don't work:

  1. Using strings.Replace to remove non-UTF8 characters 70% fail

    May corrupt data; better to use utf8.ValidString and reject or sanitize.