Search code examples
jsonpython-3.xpython-requestsprotocol-buffersgoogle-cloud-vision

What's the proper way to extract data from a google protobuff object?


I have never encountered this sort of collection or object before until now (its the response from a request to Google-Cloud-Vision API).

I wrote a class that uses the API and does what I want correctly. However the only way that I can extract/manipulate data in the response is by using this module:

from google.protobuf.json_format import MessageToJson 

I basically serialized the protobuff into a string and then used regex to get the data that I want.

There MUST be a better way than this. Any suggestions? I was hoping to have the API response give me a json dict or json dict of dicts etc... All I could come up with was turning the response into a string though.

Here is the file from the github repository: image_analyzer.py

Thank you all in advance.


Solution

  • The built in json module will parse the string into a dictionary, like json.loads(MessageToJson(response1)).