A newbie to protobuff here. I am working on compressing a JSON file using protobuff. The problem is that this JSON file comes as a response from a webserver and contains certain fields whose name are random i.e. with each request posted to the server, the key names differ. For example consider the below JSON:
{
"field1": [
{
"abc": "vala",
"def": "valb",
"ghi": "valc"
}
],
"field2": "val2",
"field3": "val3"
}
In the above json, the field names "abc", "def", "ghi" can vary each time. Is there a way in protobuf so that I get field1's value completely (like a single string or anything else) without losing the random fields inside it?
I think you want "struct.proto", i.e.
syntax = "proto3";
import "google/protobuf/struct.proto";
message Foo {
.google.protobuf.Struct field1 = 1;
string field2 = 2;
string field3 = 3;
}
or possibly (because of the array):
syntax = "proto3";
import "google/protobuf/struct.proto";
message Foo {
repeated .google.protobuf.Struct field1 = 1;
string field2 = 2;
string field3 = 3;
}
However, I should emphasize that protobuf isn't well-suited for parsing arbitrary json; for that you should use a json library, not a protobuf library.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With