The Protobuf JSON Mapping: The Four Rules That Trip People Up
You hand a protobuf message to a converter and get JSON back, and for the first five fields it just works. Then one value shows up as a number that is secretly a string, or a key you are sure you typed does not land, and the whole thing stops looking like the message you sent. There is a fixed set of rules that governs how a protobuf message becomes JSON, and it is not the same as the wire. Four of them cause almost every conversion bug, so here is what they are and why, checked against real encode and decode, not just the docs.
The first is the big integers. An int64 or uint64 does not become a JSON number, it becomes a string. A field holding 9007199254740993 comes out of the encoder as "9007199254740993", with the quotes, not as 9007199254740993. The reason is JavaScript. A number above 2 to the 53 cannot hold a thirteen digit ID exactly, and if you type that value as a plain number it quietly becomes 9007199254740992, one less, so your order ID is now a different order. The mapping sends sixty four bit integers as decimal strings to keep the digits safe, and a decoder that reads them back as numbers corrupts the last one without raising a thing. If you have ever had a user ID that was off by one in exactly the messages with the biggest IDs, this is the rule.
The second is the field names. Your .proto says user_name. The JSON says userName. The mapping converts names to lowerCamelCase on the way out, and most decoders only accept that camelCase form on the way in, so if you paste JSON that still carries the original snake_case keys the fields simply do not land and the message comes back empty with no error. That is the silent one. No exception is thrown, the field is just not there, and you end up chasing a bug that is actually a name mismatch between two files that were supposed to be the same. You can flip the output back to snake_case, the mapping allows it and the converter has a toggle for it, but know which side you are standing on before you diff two JSONs and call them different.
The third is the empty list. A repeated field with nothing in it does not become tags: [], it becomes nothing at all. An empty repeated field writes zero bytes on the wire, so after you encode and then decode, the key is just gone. If your validation insists tags must always be present, it fails on exactly the messages that have no tags, and it looks like the encoder dropped data when it only followed the spec. A repeated field with values round-trips fine, the empty one is the special case, and it is the reason a message that decodes to fewer keys than you sent is not always a bug, it can be the format working as designed.
The fourth is the enum. An enum in JSON is the member name, not the number. favorite set to BLUE comes out as the string "BLUE", even though on the wire it is the varint 2. Most decoders will also take the number on input, so favorite: 1 comes back as RED, and you can see both forms in the wild depending on who wrote the JSON. A strict check that only accepts one form will reject valid data coming from your own service. The wire never carries the name, it is a number, the name is a convenience that only exists at the JSON layer.
These four are the ones the converter follows on encode and decode, so when you paste a message and it comes back as you expected you are getting the official mapping, not a close approximation, and when it does not you are usually inside one of these rather than in your data. The two earlier posts on what protobuf is and on conversion errors name these gotchas in passing, but this is the reference for the mapping itself, and the converter at proto.datajutsu.com implements it on both sides so the JSON it hands you is the JSON a real protobuf runtime would.