Protobuf to json in python
I'd recommend using protobuf↔json converters from google's protobuf library:
from google.protobuf.json_format import MessageToJsonjson_obj = MessageToJson(org)
You can also serialise the protobuf to a dictionary:
from google.protobuf.json_format import MessageToDictdict_obj = MessageToDict(org)
Refer to the protobuf package API documentation: https://developers.google.com/protocol-buffers/docs/reference/python/ (see module google.protobuf.json_format
).
If you need to go straight to json take a look at the protobuf-to-json library, but you'll have to install that manually.
But I would recommend that you use the protobuf-to-dict library instead for a few reasons:
- It is accessible from pypi so you can simply
pip install protobuf-to-dict
or include it in arequirements.txt
dict
can be converted to json and might be more useful than a json string