Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

How to represent variable JSON data with Proto definition

57 views
Skip to first unread message

Ayush Shah

unread,
Nov 6, 2024, 7:52:28 AM11/6/24
to Protocol Buffers
Newbie question... If I have the following sample "User" JSON data:

{
  firstName: 'Bob'
  lastName: 'Smith',
  email: 'b...@gmail.com',
  metadata: {
    // a plain JS object that 
    // - will always exist and be at least an empty {} object
    // - could potentially contain any number of properties and values, depending on specific "user"
  }
}

How do I represent the metadata property within proto definition? I've tried

message User {
  message Metadata {}

  string email = 1;
  string firstName = 2;
  string lastName = 3;
  Metadata metadata = 4;
}

or using `Any`

message User {
  string email = 1;
  string firstName = 2;
  string lastName = 3;
  google.protobuf.Any details = 4;
}


but without success. Working within Node.js if relevant. Any help is much appreciated. Thanks!

Em Rauch

unread,
Nov 6, 2024, 8:34:42 AM11/6/24
to Ayush Shah, Protocol Buffers
I think what you're looking for is `google.protobuf.Struct details`; Struct is the type which allows any JS object to parse into it and acts like a `map<string, SomeJSONValueType>` (including recursively a struct inside if its another JS object in there).

--
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 visit https://groups.google.com/d/msgid/protobuf/276ff12e-a207-495a-9c92-adcc41b85b4an%40googlegroups.com.

Austin Bennett

unread,
Nov 6, 2024, 11:41:10 AM11/6/24
to Ayush Shah, Protocol Buffers
Might not work for all you need, but could try:  https://protobuf.dev/reference/protobuf/google.protobuf/#struct

In prior use cases, we've also treated things like your metadata field as a string, and then parsed the string field to extract anything relevant. 

In general, I prefer to have the proto define anything in it that might be optional, rather than [ "could be anything" ].  That allows for some nicer handling, BUT means you also would want to be in sync with the data creator/producer, which isn't always possible.  

Good luck!  

--

Ayush Shah

unread,
Nov 7, 2024, 5:48:49 AM11/7/24
to Austin Bennett, Protocol Buffers
Thanks for the response, I tried using Struct to send requests and responses but on the server side I am only receiving  the empty response every time.I checked online many people faced same issue so is it bug or implementation problem.

Any help is much appreciated. Thanks!

Em Rauch

unread,
Nov 7, 2024, 9:54:35 AM11/7/24
to Protocol Buffers
It would be extremely surprising if Struct simply did not work for generically parsing JSON; it's in very wide usage both within and outside of Google.

Do you have a reproduction or references to the other people facing issues with it?
Reply all
Reply to author
Forward
0 new messages