go data_error ai_generated true

error: unexpected end of JSON input

ID: go/encoding-json-unexpected-end-of-json-input

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.20 active
1.21 active

Root Cause

The JSON decoder reached the end of input before completing a valid JSON structure, often due to truncated data or empty input.

generic

中文

JSON解码器在完成有效JSON结构之前到达输入末尾,通常由于数据截断或空输入。

Workarounds

  1. 90% success Use io.ReadAll to read full body before decoding
    body, err := io.ReadAll(resp.Body)
    if err != nil { return err }
    if len(body) == 0 { return errors.New("empty response") }
    json.Unmarshal(body, &target)

Dead Ends

Common approaches that don't work:

  1. Assuming the error is always a server bug 50% fail

    Often caused by network issues or partial reads; retrying may help but not always.