go data_error ai_generated true

proto: (line 1:2): unknown field "user_id" in pb.User

ID: go/proto-unknown-field-json

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
google.golang.org/protobuf 1.26+ active

Root Cause

protojson.Unmarshal encountered a JSON field not present in the target message. Common when JSON uses snake_case but the proto field uses camelCase (protojson default), or the JSON came from a different schema version.

generic

中文

protojson.Unmarshal 遇到目标消息中不存在的 JSON 字段。常见于 JSON 使用 snake_case 而 proto 字段使用 camelCase(protojson 默认),或 JSON 来自不同 schema 版本。

Workarounds

  1. 90% success
    Use protojson with explicit options:
    opts := protojson.UnmarshalOptions{
        DiscardUnknown: true,
    }
    err := opts.Unmarshal(data, msg)
  2. 85% success
    Configure the emitter/unmarshaler for the naming convention you expect:
    protojson.UnmarshalOptions{Resolver: ...}
    // and emit with UseProtoNames to force snake_case:
    m := protojson.MarshalOptions{UseProtoNames: true}

Dead Ends

Common approaches that don't work:

  1. 85% fail

    protojson returns a partial message; downstream code sees zero values and produces wrong results.

  2. 90% fail

    Generated protobuf structs do not honor json tags; protojson uses its own field-name mapping.