Protobuf to json in python

exHash picture exHash · Nov 1, 2013 · Viewed 62k times · Source

I have an object that I de-serialize using protobuf in Python. When I print the object it looks like a python object, however when I try to convert it to json I have all sorts of problems.

For example, if I use json.dumps() I get that the object (the generated code from protoc) does not contain a _ dict _ error.

If I use jsonpickle I get UnicodeDecodeError: 'utf8' codec can't decode byte 0x9d in position 97: invalid start byte.

Test code below is using jsonpickle with the error shown above.

if len(sys.argv) < 2:
    print ("Error: missing ser file")
    sys.exit()
else :
    fileLocation = sys.argv[1]

org = BuildOrgObject(fileLocation) 

org = org.Deserialize()


#print (org)
jsonObj = jsonpickle.encode(org)
print (jsonObj)

Answer

denis-sumin picture denis-sumin · Sep 25, 2016

I'd recommend using protobuf↔json converters from google's protobuf library:

from google.protobuf.json_format import MessageToJson

jsonObj = MessageToJson(org)

Refer to protobuf package API: https://developers.google.com/protocol-buffers/docs/reference/python/ (see module google.protobuf.json_format).

Note you can also serialise the protobuf to a Dict

from google.protobuf.json_format import MessageToDict
dict_obj = MessageToDict(org)