JSON field to gRPC field mapping

535 views
Skip to first unread message

Josh Morehead

unread,
Nov 15, 2021, 6:52:01 PM11/15/21
to Protocol Buffers
I am wondering if there is a way to map a JSON field to a Go struct field of a different name??

For instance... can I put in my .proto an object with the field `status` and have that map to a JSON field of `statusCode`?? This way when I unmarshal the JSON the value at `statusCode` would appear in the Go struct field `status`.

The only place I know to control that would be in the declaration of the struct housing the field... but in this case that is autogenerated code which appears to use the .proto field to create the mapped JSON field. Any help would be much appreciated.

Derek Perez

unread,
Nov 15, 2021, 6:56:54 PM11/15/21
to Josh Morehead, Protocol Buffers
I believe you can annotate the struct field using syntax similar to:

status string `json:"statusCode"`

for example.

 - D

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/80d8024a-90ce-4dda-9a27-8839e332096dn%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/80d8024a-90ce-4dda-9a27-8839e332096dn%40googlegroups.com.

Josh Morehead

unread,
Nov 16, 2021, 1:06:08 PM11/16/21
to Protocol Buffers
Yes... but in this case that would be in the generated protobuf code. Which would get over written anytime they are invoked. I guess I'm trying to find a place other that??

Derek Perez

unread,
Nov 16, 2021, 1:08:24 PM11/16/21
to Josh Morehead, Protocol Buffers

Josh Morehead

unread,
Nov 17, 2021, 11:15:21 AM11/17/21
to Protocol Buffers
Derek,

That looks like it'll do exactly what I'm looking for.
I'm in transit for work travel butI will definitely check this out the min I land and get some time.

Thanks!! I'll post back what happens. 

David Raleigh

unread,
Nov 17, 2021, 11:58:40 AM11/17/21
to Josh Morehead, Protocol Buffers
I'm interested in the same feature. I'm curious to know if there are any good tutorials about using the descriptor.proto to map field names between protobuf field and json field. 

Or it doesn't have to be a tutorial, it could just be a proto field I can use to experiment with.

I'm struggling with json field names that contain ":" or "@". 

Josh Morehead

unread,
Nov 19, 2021, 11:52:00 AM11/19/21
to Protocol Buffers
Hey all... Finally got to test this out. So far it's not working. Trying to dig in deeper to see if there's something I'm missing. Not much on this out there sadly.
Thus far I've tried the below:

```
string customerCode = 14;
string status = 15 [json_name="statusCode"];
string workStateTaxCode = 16;
```

Any ideas or directions as always would be much appreciated. :)

Derek Perez

unread,
Nov 19, 2021, 11:53:12 AM11/19/21
to Josh Morehead, Protocol Buffers
Can you describe what you're seeing? I am not sure what it means to not work.

Josh Morehead

unread,
Nov 19, 2021, 11:57:10 AM11/19/21
to Protocol Buffers
Derek,

It's actually doing nothing at this point. I added that tag and it changed nothing. The hope was that the tag would make it so that the ""statusCode field on the incoming JSON would map to the "status" field on my internal gRPC object.

Derek Perez

unread,
Nov 19, 2021, 12:08:42 PM11/19/21
to Josh Morehead, Protocol Buffers

Josh Morehead

unread,
Nov 22, 2021, 11:15:19 AM11/22/21
to Protocol Buffers
Derek,

Hard to say. This functionality feels overly complicated to me.
I'm surprised there hasn't been an obvious and easy fix for something like this honestly.

Still poking at things with no luck thus far.

Josh Morehead

unread,
Nov 22, 2021, 12:40:00 PM11/22/21
to Protocol Buffers
So as I've gotten deeper this functionality does not do what I need sadly.

Which puts me back a square one... hoping to find something in the protobuf ability that allows me to alter the "json" section of the following code generated by the protoc command.

Status string  `protobuf:"bytes,15,opt,name=status,proto3" json:"status,omitempty"`

If I could figure out how to get protoc to generate say statusCode while setting my gRPC object to still have Status. Hopefully that clears up any confusion from the thread thus far.

Reply all
Reply to author
Forward
0 new messages