What is Protobuf?
Protobuf is short for Protocol Buffers. It is a way for software to store or send data as binary messages. A .proto file describes the message types and fields so the software reading a message knows what the bytes mean.
What is a .proto file?
A .proto file gives each field a name, a type and a number. The binary message uses the numbers to identify the fields. In our example, field 1 of Contact holds the ID and field 2 holds the name. Keep those numbers stable when editing a schema used by other programs.
One .proto file can describe several messages. Pick the type that matches the bytes you are reading. In our sample, Contact holds one person and Contacts holds a list of those people. The word repeated lets that field hold more than one entry.
Example contacts and message types
This sample stores Avery and Ellis in one Contacts message. The same two records appear in the other guides. Here the JSON has a named contacts field because the converter expects one message object at the top.
{
"contacts": [
{"id": 42, "name": "Avery"},
{"id": 51, "name": "Ellis"}
]
}syntax = "proto3";
message Contact {
int32 id = 1;
string name = 2;
}
message Contacts {
repeated Contact contacts = 1;
}When is Protobuf useful?
Protobuf is useful when apps or services send messages to each other and already share a schema. A service might send a list of contacts and another service can read it using the same .proto definition. The binary format leaves out field names, which can keep repeated messages compact.
Use JSON if people need to edit the message by hand or the receiving system expects plain JSON. ProtoJSON is the standard way to write a Protobuf message as JSON. It has rules for types that ordinary JSON leaves open. For instance, 64-bit integer fields appear as quoted numbers so they keep all their digits.
How do I convert it?
In Protobuf to JSON, open the .bin file, paste the .proto definition into Protobuf schema, and enter Contacts as the message type. Select Convert to JSON. The output shows the named fields and both contacts.
In JSON to Protobuf, paste the sample ProtoJSON and use the same schema and message type. Select Convert to Protobuf and download the binary message. Reopen it in the other converter to check the result.
What happens when the schema changes?
A field number tells the binary reader which field it is reading. If you reuse an old number for a new field, a reader can misunderstand older data. Reserve the numbers of removed fields so they stay unused.
Use a copy of the sender’s schema when you inspect a file. The wrong schema may still produce output that looks valid. Check a few known values before relying on the result, especially if two message types use the same field numbers.
What should I check if conversion fails?
Use the .proto definition from the program that wrote the file. A binary message usually does not say which schema or type it uses. If the schema declares a package, include it in the message name, for example example.Contacts.
This tool reads one message at a time and needs the definitions in the schema input. If the source gives you a stream, a gRPC frame or a schema with external imports, ask for a single message and a schema with the needed definitions available in one file.