Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a proto file for a JSON input containing random field names

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?

like image 641
PanDroid Avatar asked Oct 31 '25 18:10

PanDroid


1 Answers

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.

like image 173
Marc Gravell Avatar answered Nov 03 '25 09:11

Marc Gravell