ProtoJSON is the JSON form of a message, defined by the
Protocol Buffers
documentation. It is not used in .pb files but is the usual way of
showing a message to people. A parser for it needs the schema, and field names must
be known; the explorer does not read ProtoJSON files, which are ordinary
JSON.
Each type
| Protocol Buffers | JSON | Example | Notes |
|---|---|---|---|
| message | object | {"fooBar": v} | Keys are lowerCamelCase field names or json_name. null is accepted for any field and leaves it unset. |
| enum | string | "FOO_BAR" | The value’s name. Parsers also accept numbers. With aliases, the first listed name is written and all are accepted. |
map<K,V> | object | {"k": v} | All keys become strings. |
repeated V | array | [v, …] | |
| bool | boolean | true | |
| string | string | "Hello" | |
| bytes | string | "YWJj" | Base64; parsers accept the standard or URL-safe alphabet, with or without padding. |
| int32, fixed32, uint32 | number | 1 | Parsers also accept strings. Exponents are allowed. |
| int64, fixed64, uint64 | string | "1" | Decimal strings, to avoid loss of precision. Parsers also accept numbers. |
| float, double | number or string | 1.1, "NaN" | Special values are the strings "NaN", "Infinity" and "-Infinity". |
Well-known types
These messages have special forms in JSON.
| Message | JSON | Example | Notes |
|---|---|---|---|
| Any | object | {"@type": "url", "f": v} | An @type key holds the type_url. A type with a special mapping of its own goes under "value". |
| Timestamp | string | "1972-01-01T10:00:20.021Z" | RFC 3339, written in UTC with 0, 3, 6 or 9 decimal digits. |
| Duration | string | "1.000340012s" | Seconds and a final s; not RFC 3339’s form. |
| Struct | object | {…} | Any JSON object. |
| Wrapper types (BoolValue, Int32Value, StringValue and the rest) | the wrapped value’s | 2, true, null | As the primitive, but null is allowed and kept. |
| FieldMask | string | "f.fooBar,h" | Comma-separated camelCase paths. |
| ListValue | array | [foo, bar] | |
| Value | any JSON value | ||
| NullValue | null | null | |
| Empty | object | {} | A message with no fields. |
Presence
Fields with presence are written exactly when they are set. Fields without
presence (scalars without explicit presence, repeated fields, maps) are left out at
their default. On parsing, null leaves a field unset; it is not allowed
inside repeated fields, except for a value of the NullValue type.
The feature json_format
decides whether a schema must have a well-defined mapping.