How to pass in json as payload in .proto
Asked Answered
E

5

6

As per the following page I should be able to send in json payload : https://developers.google.com/protocol-buffers/docs/proto3 under 'JSON Mapping'.

I would like to send in json payload as part of the message and I have the following .proto file :

message EventsRequest{
    message RequestElement {
        struct payload = 1;
    }
    string customerId = 1;
    repeated RequestElement jsonPayload = 2;
}


message EventsResponse {
    int32 status = 1;
    string rawResponseData = 2;
    struct responseData = 3;
}

But compiling it gives me the following error :

[INFO] Compiling 1 proto file(s) to C:\workspace\...\target\generated-sources\protobuf\java
[ERROR] PROTOC FAILED: msg_service.proto:21:9: "struct" is not defined.
msg_service.proto:34:5: "struct" is not defined.

[ERROR] C:\workspace\...\src\main\proto\msg_service.proto [0:0]: msg_service.proto:21:9: "struct" is not defined.
msg_service.proto:34:5: "struct" is not defined.

I have tried 'Struct' also, but I got the same error.

Am I misunderstanding usage ? If I have to send in json payload, do I pass in as a string ?

Thanks

Embosser answered 15/12, 2016 at 1:4 Comment(0)
E
13

I finally ended up using String to represent json payload.

Embosser answered 5/1, 2017 at 23:22 Comment(1)
This is the answer, honestly.Bigamist
F
3

If you would like to use a Struct you will need to first import:

import "google/protobuf/struct.proto";

Then during the declaration instead of just the word Struct use google.protobuf.Struct

Fabozzi answered 9/7, 2018 at 20:35 Comment(0)
P
1

It should be like this,

syntax = "proto3";

package db;
import "google/protobuf/struct.proto";

service Proxy
{
    rpc appConfig(UserId) returns (AppConfig);
}

message UserId
{
    string userId= 1;
}

message AppConfig
{
    Struct appConfig = 1;
}


Primogenitor answered 26/8, 2019 at 7:1 Comment(0)
B
1

I think the issue is it is not able to import the right message from google/protobuf/struct.proto , so I used

google.protobuf.Struct field_name = 1

and it worked for me !!!

Beldam answered 30/12, 2021 at 7:17 Comment(0)
E
0

It should be Struct, with a capital S.

Engird answered 15/12, 2016 at 1:21 Comment(3)
That is the first thing that I tried and I got the same error, so I tried with 'struct'Embosser
I think you also need: import "google/protobuf/struct.proto";Engird
I tried the import also, but that did not work either.Embosser

© 2022 - 2024 — McMap. All rights reserved.