There is no Protocol Buffer schema that corresponds to the given JSON. The JSON support in Protocol Buffers is really intended for defining JSON serialization for existing Protocol Buffer messages. You can't use Protocol Buffers as a schema for arbitrary JSON, it's simply not designed to do that.
In particular, Protocol Buffers lets you define an array as a repeated field, but the field type must have some non-array type, like a message, primitive, or enum.
The best you can do is change the schema:
message Name {
repeated string name = 1;
}
message Link {
string id = 1;
repeated string names = 2;
}
And then you can change the JSON to match:
const jsonStr = `
{
"names": [
{ "name": ["Bill", "Susan"] },
{ "name": ["Jim", "James"] }
]
}
`
func main() {
var link pb.Link
if err := jsonpb.UnmarshalString(jsonStr, &link); err != nil {
fmt.Println("Error:", err)
}
}
If your JSON format is a hard requirement, then you must deserialize the JSON in some other way.