2
{
    "response": {
        "status": [
            {
                "code": "red",
                "text": "random sentence"
            },
            {"code": "blue"}
        ],
        "recent": []
    }
}

For above json, I want to have proper protobuf syntax. Below is just an outline to show the idea. I know that below proto won't work.

  syntax = 'proto3';

  map< string, repeated map <string, string > > response = 1;

How can I write this in protobuf syntax?

1 Answer 1

1

You cannot; the thing on the right in a map cannot be repeated; you'd instead have a map<string, Foo> for some Foo that *hasarepeated` something.

Honestly, I think the key problem here is this statement:

For above json, I want to have proper protobuf syntax.

Protobuf is not a general purpose JSON tool; the JSON it chooses is strongly opinionated. If you want to map pre-existing JSON, then protobuf is not the right tool for you; instead, use any general purpose JSON tool - they'll usually be fine with it. Instead, think of protobuf as a serialization tool that happens to have a JSON variant output (in addition to the primary binary output), but which offers very little flexibiltiy re JSON layout.

Sign up to request clarification or add additional context in comments.

2 Comments

message Extra { map<string, string> infos = 1; } message ExtraList { repeated Extra extra_list = 1; } map<string, ExtraList> response = 16; I was able to make my proto this way but I just want a list of dicts without naming each variable. Like in the json representation, "extra_list" and "infos" need to be added. Do you know any other alternative to do this?
@DivyanshuShekhar no, basically

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.