How to Generate a .proto File From JSON (and Prove It Works)

You have a pile of JSON and you need a .proto file. No schema exists, nobody will give you one, and the alternative is to eyeball every field and guess the types. People do this all the time, and it is how bugs get born: a field you declared int32 because your sample values were small, then a production record with a 64-bit ID comes through and the number silently truncates. Guessing from a sample never told you what the sample was not showing you. The honest fix is mechanical, and it has a verification step at the end that most people skip.

Step one is inference, and it is just type mapping done carefully. A JSON string becomes a string field, a number becomes int32 unless the value is bigger than 2 to the 31st power (then int64), a decimal becomes double, a bool becomes bool, an array becomes repeated, and an object becomes its own message with its own fields. Field numbers go in the order the keys first appear. Where real care is needed is multiple records: if you paste an array of objects, the types get merged across all of them and widened when they disagree, one record with age 42 and another with 3 billion becomes an int64, not an int32. A tool that only looks at the first record will be confidently wrong about the rest of your data.

Step two is the part that actually earns the schema its trust, because inference from a sample can only prove the schema fits that sample. The check is a round trip: take your original JSON, encode it into binary using the .proto you just drafted, decode the binary back to JSON, and diff the two field by field. Clean match means the schema genuinely carries your data. A warning for an empty list that came back missing is normal, not a bug: an empty repeated field encodes to zero bytes and has nothing to come back as, that is how the format works. A real failure, like a number that came back smaller or an array that changed length, means the draft is wrong somewhere and tells you exactly which field. Run the round trip before you hand the schema to a compiler, not after the first incident.

The practical workflow is short. Paste the JSON, let it draft the .proto, run the verify round trip, then copy or download the schema. No protoc install, no build environment, no editor plugin. The JSON to .proto tab on proto.datajutsu.com does all of that in the browser and nothing you paste ever leaves the page. The drafted schema is plain protobuf text, so it drops straight into protoc, gRPC codegen, or the same site’s other tabs (decode with the schema, generate typed clients in TypeScript, Python, Go, or Rust) the moment it passes verification.

If you are new to the format, start with what protobuf actually is and how the wire format works. And if you have already crossed the other direction: converting a .pb file to JSON with the schema, encoding JSON back to binary, or reading a .pb file with no schema at all.

Subscribe to Data Jutsu

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe