Convert a Protobuf into array of JSONs

19 views
Skip to first unread message

Guilherme Costa

unread,
Apr 22, 2020, 8:18:24 PM4/22/20
to Protocol Buffers
I have a question regarding Protobufs, since I want to generate an array of JSONs by using them, such as in this example
[
   {
    "var": ["myVariable1","myVariable2"], 
    "key1": 123123,
    "key2": 1122,
    "key3": "abcd-0101"
   },
  { 
    "var": ["myVariable1"], 
    "key1": 123124,
    "key2": 1123,
    "key3": "abcd-0102"
  },
] 

However, I just can get the example below:
{
list: [
{ "var": ["myVariable1","myVariable2"], "key1": 123123, "key2": 1122, "key3": "abcd-0101" }, { "var": ["myVariable1"], "key1": 123124, "key2": 1123, "key3": "abcd-0102" }, ]
 
}

It is possible to generate the first example (without the brackets and the first key) using protobufs?
Some of this issue are also discussed in: https://stackoverflow.com/questions/46945555/how-to-model-json-array-as-protobuf-definition, they also had the same question, but I tried and did not work at all!


David L. Jones

unread,
Apr 23, 2020, 7:42:50 PM4/23/20
to Protocol Buffers
The fundamental unit for protobufs (across all serialization and presentation formats, including JSON) is a message.

The semantics of the general JSON format do not map to protobuf semantics very well, but the proto3 language guide has a section on the JSON structures that can interoperate with protobufs:
https://developers.google.com/protocol-buffers/docs/proto3#json

The impetus for JSON interop is for servers which can understand either JSON- or wire-formatted requests, so a top-level list would pretty fundamentally break semantics. Given that pitfall, it's pretty unlikely that we would add support for anything other than a top-level message. The example you gave is how I would recommend representing "just" a list.

Crouse, Kerry

unread,
Apr 23, 2020, 8:11:05 PM4/23/20
to prot...@googlegroups.com

David,

    Thank you for your very prompt reply! I am required (for compatibility) to use version 2.6.0. I'm not sure this makes much of a difference. What I am attempting to ultimately accomplish is turn the .proto files into code I can feed into the PowerPC cross compiler to generate code as part of the static image to load onto the PowerPC. Perhaps I'm not thinking about this process in the correct manner. If I generate Protoc using that cross compiler, Protoc will have to be run on the PowerPC? If so, my goal is defeated. I don't think, however, that running the x86 Protoc will give me the correct data structures and code. Perhaps I'm completely backwards.

        Kerry

On 4/23/2020 7:42 PM, 'David L. Jones' via Protocol Buffers wrote:
The fundamental unit for protobufs (across all serialization and presentation formats, including JSON) is a message.

The semantics of the general JSON format do not map to protobuf semantics very well, but the proto3 language guide has a section on the JSON structures that can interoperate with protobufs:
https://developers.google.com/protocol-buffers/docs/proto3#json

The impetus for JSON interop is for servers which can understand either JSON- or wire-formatted requests, so a top-level list would pretty fundamentally break semantics. Given that pitfall, it's pretty unlikely that we would add support for anything other than a top-level message. The example you gave is how I would recommend representing "just" a list.

On Wednesday, April 22, 2020 at 5:18:24 PM UTC-7, Guilherme Costa wrote:
--
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/8a095f8d-a572-41fb-8fb7-72de6208874e%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages