How to convert from Json to Protobuf? How to convert from Json to Protobuf? json json

How to convert from Json to Protobuf?


With proto3 you can do this using JsonFormat. It parses directly from the JSON representation, so there is no need for separately calling MyMessage.parseFrom(...). Something like this should work:

JsonFormat.parser().merge(json_string, builder);


//You can use this for converting your input json to a Struct / any other Protobuf Class    import com.google.protobuf.Struct.Builder;import com.google.protobuf.Struct;import com.google.protobuf.util.JsonFormat;import org.json.JSONObject;JSONObject parameters = new JSONObject();Builder structBuilder = Struct.newBuilder();JsonFormat.parser().merge(parameters.toString(), structBuilder);// Now use the structBuilder to pass below (I used it for Dialog Flow V2 Context Management)


online service:https://json-to-proto.github.io/

This tool instantly converts JSON into a Protobuf. Paste a JSON structure on the left and the equivalent Protobuf will be generated to the right, which you can paste into your program. The script has to make some assumptions, so double-check the output!