go data_error ai_generated true

proto: invalid value for int32 field: "abc"

ID: go/protobuf-json-marshal-invalid-value

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2025-02-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.x active — — —

Root Cause

The JSON value provided for an integer field is not a valid integer, causing protojson unmarshaling to fail.

generic

中文

为整数字段提供的 JSON 值不是有效的整数,导致 protojson 解组失败。

Workarounds

  1. 90% success
    // Ensure the JSON field is a number, not a string.
    // If it's a string, parse it and set the field manually.
  2. 85% success
    opts := protojson.UnmarshalOptions{AllowPartial: true}
    err := opts.Unmarshal(data, msg)

Dead Ends

Common approaches that don't work:

  1. 80% fail

    Changing the type breaks compatibility and may not be semantically correct.

  2. 70% fail

    Custom parsers are error-prone and don't integrate with protobuf well.