> ## Content Index
> Fetch the complete content index at: https://datajutsu.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# A Oneof Is a Group of Ordinary Fields (and the Group Key Is Not on the Wire)
- URL: https://datajutsu.com/protobuf-oneof-json-group-key/
- Published: 2026-11-01T21:56:46.000Z
- Updated: 2026-11-01T21:56:46.000Z
- Author: Data Jutsu

Start with the bytes, because the oneof is the part of the language where people assume the most machinery. I took a message with a oneof called contact, holding email at 1, phone at 2 and ext\_id at 3, and I encoded email with the value a@b.c. The bytes are 0a056140622e63: the tag for field 1, the length, the string. Then I encoded the same value in an identical message where the three fields are plain fields and there is no oneof keyword anywhere. Same seven bytes. Phone at 2 with the value 555 is 10ab04 either way. A oneof member on the wire is a normal field wearing a group name from your .proto, and the wire never saw the group. The tag and varint math underneath is in the [wire format post](https://datajutsu.com/how-protobuf-wire-format-works/?ref=datajutsu.com), and the oneof's wire side is walked in depth in the [oneof post](https://datajutsu.com/protobuf-oneof/?ref=datajutsu.com).

The exclusivity is a promise the language makes to your code, not to the bytes. I encoded JSON that names both email and phone at once, and the engine wrote both tags, 0a056140622e6310ab04, with no exclusivity check anywhere in the encoder. Then I fed a raw stream carrying both tags back through the decode tab. It decoded without a word of complaint, and both values survived into the JSON: the email string, the phone string, and the group key, which comes in a moment. If a sender has a bug that writes both members, your receiver's decoder will not catch it for you. The contract lives in the compiler and in your client, and the bytes just carry two ordinary fields.

The group key is the part of the output people trip over. With the decode tab's options, that two-member stream prints as an object where email and phone carry their values and a third key, contact, the name of the oneof, carries the string phone. The group key does not hold a value; it holds the name of the member that won. A single-member message prints the same shape: email with its value, and contact set to email. The key is a pointer to a member, spelled out in the JSON, so the output stays self-describing without the reader needing to know what a oneof is.

The winner is not the last thing on the wire, and this is the one I did not expect. I reversed the stream, phone first, email after. The group key still says phone. It is field number order, not arrival order: phone at 2 beats email at 1, and when I add a third set member with a higher number the group key flips to it. The group key is pure presence. An unset oneof prints no member keys and no group key at all, while the plain fields in the same object, under the same defaults option, print their default values. A oneof that was never set is the one shape in the JSON where absence is actually visible, which runs against the rest of the presence story in the [defaults post](https://datajutsu.com/protobuf-defaults-are-not-on-the-wire/?ref=datajutsu.com).

The encoder's door is the mirror image, and it is stricter than it looks. The group name is not a key it accepts: JSON shaped as contact holding an email encodes to nothing, because the member key is read at the top level only. Unknown keys are dropped silently; I put a fax in and it never reached the wire. A member explicitly set to zero is the exception that proves the presence rule: the two bytes 10 00 decode to a with the number 0 and g set to a, member key and group key both present, where the same value on a plain field prints nothing at all. One more quiet fact: the tool's camelCase toggle does not touch these keys in this build, the engine reads the case flag only at parse time and the tool parses with the original names kept, so the member key and the group key always share the same spelling, whatever the dropdown says.

So the rules: the oneof is a group of ordinary fields on the wire, no marker and no exclusivity, and a sender bug that writes both members passes straight through a clean decode; the group key your decoder prints is a presence label naming the winner, and the winner is the highest field number, not the last write; an unset oneof is the one place in the JSON where you can tell a field was never sent; the encoder reads member keys at the top level, drops everything else, and prints an explicit zero where it would skip a plain field. You can run all of it against your own schema in the [tool at proto.datajutsu.com](https://proto.datajutsu.com/?ref=datajutsu.com): put a oneof in, decode a stream with two members set, and watch the group key name the higher number. The JSON rules around the whole thing are in the [JSON mapping post](https://datajutsu.com/protobuf-json-mapping/?ref=datajutsu.com).