79469312

Date: 2025-02-26 10:13:45
Score: 1
Natty:
Report link

There is now a built-in method method to recursively convert a Struct to native python objects: google.protobuf.json_format.MessageToDict()

Here's the documentation for this method: https://googleapis.dev/python/protobuf/latest/google/protobuf/json_format.html

from google.protobuf.json_format import MessageToDict
from google.protobuf.struct_pb2 import Struct

struct = Struct()
struct.update({"hi": 1, "over": [23, 45, None, 6.7, "there", [8.9, "10"], {"key": None}]})
out = MessageToDict(struct)
print(out)

Output:

{'hi': 1.0, 'over': [23.0, 45.0, None, 6.7, 'there', [8.9, '10'], {'key': None}]}
Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Julie