0

I am using grpc-gateway and I would like to expose an api with the following json response:

{
  "id": "123",
  "name": "MyItem",
  "properties": {
    "propA": 1,
    "propB": "some value",
    "propC": true,
  }
}

The values within "properties" are dynamic and not known ahead of time.

So far, my proto definition is:

message Item {
  message PropertyValue {
    string string_value = 1;
    int32 number_value = 2;
    bool bool_value = 3;
  }
  
  string id = 1;
  string name = 2;
  map<string, PropertyValue> properties = 3; 
}

But this yields a response like this:

{
  "id": "123",
  "name": "MyItem",
  "properties": {
    "propA": {
      "number_value": 1
    },
    "propB": {
      "string_value": "some value"
    },
    "propC": {
      "bool_value": true
    }
  }
}

How can I modify my proto so that properties is returned as a json map without the additional typing information?

Jason
  • 99
  • 6

1 Answers1

0

This answer pointed me to google.protobuf.Struct. So I changed my message to be:

message Item {
  string id = 1;
  string name = 2;
  google.protobuf.Struct properties = 3; 
}

And this achieves what I wanted.

Jason
  • 99
  • 6