1 year ago

#374851

test-img

Belphegor

Protobuf - json: cannot unmarshal string into Go struct field of int32

I am trying to unmarshall the contents of my json file and one of the attribute fields is this:

"sensor_node_id": 6,

Inside my .proto file I have:

message Attributes1 {
    // delete above attributes because too many and just want to show what I define here
    int32 sensor_node_id = 12;
    // deleted below remaining attributes
}

message Attributes {
    Attributes1 attributes = 1;
    string message = 2;
    string service = 3;
    string status = 4;
    repeated string tags = 5;
    string timestamp = 6;
}

and then when I generate my protobuf file I get this struct:

type Attributes1 struct {
    SensorNodeId     int32             `protobuf:"varint,12,opt,name=sensor_node_id,json=sensorNodeId,proto3" json:"sensor_node_id,omitempty"`

}

Then when I run my program I see this error:

2022/04/04 13:16:15 json: cannot unmarshal string into Go struct field Attributes.data.attributes.attributes.sensor_node_id of type int32

Would anyone be kind enough to help with this problem. I spent a few hours googling how to resolve this and it seems not as simple as I thought.

I tried changing it to "string" after actually but then I get the error which goes along the lines of something like:

Cannot unmarshal a number into a string....

json

go

protocol-buffers

unmarshalling

0 Answers

Your Answer

Accepted video resources