I am implementing a gRPC API and wanted to add JSON body data as it is in response.
so I have tried:
type Message struct {
Subject string `json:"subject"`
Body interface{} `json:"body"`
}
proto3
message Message {
string subject = 1;
string body = 2;
}
API code:
en, err := client.Request.Get(req.Name)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
data, _ := json.Marshal(en.Body)
return &response.Message{
Subject: en.Subject,
Body: string(data),
}, nil
After adding this getting gRPC API response:
{
"subject": "dev",
"body": "{\"name\":\"environment\",\"description\":\"The default environment\"}",
}
The problem is body JSON key-value is dynamic. Is there any way by which we can get response something like
{
"subject": "dev",
"body": {"name":"environment","description":"The default environment"},
}