Adam Matan Adam Matan - 4 years ago 242
JSON Question

Protobuf3: Serialize a Python object to JSON

According to the manual, Protobuf 3.0.0 supports JSON serialization:


A well-defined encoding in JSON as an alternative to binary proto encoding.


What have I tried


  • json.dumps(instance)
    which raised
    TypeError(repr(o) + " is not JSON serializable")

  • Looked for a
    instance.to_json()
    (or alike) function

  • Searched the Python docs



How do I serialize a Python proto object to JSON?

Answer Source

There is a function MessageToJson in the json_format module. This function can be used to serialize the message.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download