undefine97
undefine97

Reputation: 177

JSON to Protobuf (v3)

Noob question here. I'm still new to the Protobuf thing. How can I convert my JSON data to a Protobuf JSON?

{
    "id": "12345678901233456",
    "name": "asdfghjkl",
    "start_time": 1.666058486989E9,
    "trace_id": "1-adg346-dhagjkh348762834jah37846f",
    "end_time": 1.666058487934E9
}

I'm trying to send the fetched data from the AWS X-Ray which is the trace information to an OTLP HTTP endpoint (0.0.0.0:4318). And it should be a dynamic thing because it is not certain if it will have the same structure or not because it depends on the fetched trace information given. The serialization format needs to be protobuf JSON.

Upvotes: 1

Views: 4594

Answers (1)

Mayukh Sarkar
Mayukh Sarkar

Reputation: 2615

You can use google.golang.org/protobuf/encoding/protojson to convert your JSON file into protobuf.

req := &proto.JobCreateRequest{}
err := protojson.Unmarshal(bytes, req)

You need to manually convert json into bytes. This will give you a proto request with a proto message.

Upvotes: 3

Related Questions