Protobuf field guide

Convert Protobuf to JSON safely.

A JSON object can look right while representing the wrong value. These are the type rules to check before you encode or edit a decoded message.

Use a small schema to test each type.

Load this definition into the schema workspace. The matching JSON uses a decimal string for the large integer, an enum name, Base64 for bytes and a single member of the oneof.

Protocol definition & example
syntax = "proto3";
message Record {
  int64 record_id = 1;
  enum State { UNKNOWN = 0; READY = 1; }
  State state = 2;
  bytes data = 3;
  oneof contact {
    string email = 4;
    string phone = 5;
  }
}

Preserve the value, not just the appearance.

The integer 9007199254740993 is larger than JavaScript can represent exactly as a Number. Quotes keep its decimal digits intact until the Protobuf encoder converts them into a 64-bit integer.

JSON example
{
  "record_id": "9007199254740993",
  "state": "READY",
  "data": "SGk=",
  "email": "dev@example.com"
}

Check the mapping for each field.

Protobuf typeJSON in this workspaceWatch for
int64 / uint64Decimal string, or a safe integer numberQuoted strings preserve large values; signed and unsigned ranges differ.
enumDeclared name or an int32 numeric valueUnknown names are rejected; numeric values can preserve newer sender values.
bytesPadded Base64 stringSGk= represents the bytes 48 69, not the literal characters S G k =.
repeatedJSON arrayEach entry must match the element type.
mapJSON objectKeys are strings in JSON, including decimal 64-bit keys.
oneofOne member at a timeRemove the old member before setting a different one.

Know which JSON format you are using.

The official ProtoJSON format normally prints field names in lowerCamelCase and represents 64-bit integers as strings. This workspace uses protobufjs with keepCase enabled, so input and output retain the field spelling in your .proto file: record_id remains record_id.

Decoded output includes default values and may include a oneof discriminator such as "contact":"email". The workspace accepts that discriminator when it agrees with the populated member. These are workspace conventions, not a promise of full canonical ProtoJSON compatibility.

A displayed zero or empty string does not always mean that a sender explicitly set the field. Presence depends on the declaration, including optional and oneof. Consult your schema before treating default-filled JSON as a presence report.

Fix the input rather than coercing it.

  • Do not turn large int64 strings into Number values while editing.
  • Use a boolean true or false for bool fields, not quoted text.
  • Use a numeric JSON value for int32 fields; the workspace rejects numeric strings there.
  • Do not populate both email and phone in the example oneof.
  • Keep the original binary when unknown fields or exact original serialization must be retained; converting through JSON can discard information.

Reference material

Use the specifications for details beyond these examples.