python - How can I JSON serialize an object from google's natural language API? (No __dict__ attribute) -
i'm using google natural language api project tagging text sentiment analysis. want store nl results json. if direct http request made google json response returned.
however when using provided python libraries object returned instead, , object not directly json serializable.
here sample of code:
import os import sys import oauth2client.client google.cloud.gapic.language.v1beta2 import enums, language_service_client google.cloud.proto.language.v1beta2 import language_service_pb2 class languagereader: # class parses, stores , reports language data text def __init__(self, content=none): try: # attempts autheticate credentials env variable oauth2client.client.googlecredentials.get_application_default() except oauth2client.client.applicationdefaultcredentialserror: print("=== error: google credentials not authenticated! ===") print("current enviroment variable process is: {}".format(os.environ['google_application_credentials'])) print("run:") print(" $ export google_application_credentials=/your_path_here/your_json_key_here.json") print("to set authentication credentials manually") sys.exit() self.language_client = language_service_client.languageserviceclient() self.document = language_service_pb2.document() self.document.type = enums.document.type.plain_text self.encoding = enums.encodingtype.utf32 self.results = none if content not none: self.read_content(content) def read_content(self, content): self.document.content = content self.language_client.analyze_sentiment(self.document, self.encoding) self.results = self.language_client.analyze_sentiment(self.document, self.encoding)
now if run:
sample_text="i love r&b music. marvin gaye best. 'what's going on' 1 of favorite songs. sad when marvin gaye died." resp = languagereader(sample_text).results print resp
you get:
document_sentiment { magnitude: 2.40000009537 score: 0.40000000596 } language: "en" sentences { text { content: "i love r&b music." } sentiment { magnitude: 0.800000011921 score: 0.800000011921 } } sentences { text { content: "marvin gaye best." begin_offset: 18 } sentiment { magnitude: 0.800000011921 score: 0.800000011921 } } sentences { text { content: "\'what\'s going on\' 1 of favorite songs." begin_offset: 43 } sentiment { magnitude: 0.40000000596 score: 0.40000000596 } } sentences { text { content: "it sad when marvin gaye died." begin_offset: 90 } sentiment { magnitude: 0.20000000298 score: -0.20000000298 } }
which not json. it's instance of google.cloud.proto.language.v1beta2.language_service_pb2.analyzesentimentresponse object. , has no __dict__ attribute attribute not serializable using json.dumps().
how can either specify response should in json or serialize object json?
edit: @zach noted google's protobuf data interchange format. seems preferred option use these protobuf.json_format
methods:
from google.protobuf.json_format import messagetodict, messagetojson self.dict = messagetodict(self.results) self.json = messagetojson(self.results)
from docstring:
messagetojson(message, including_default_value_fields=false, preserving_proto_field_name=false) converts protobuf message json format. args: message: protocol buffers message instance serialize. including_default_value_fields: if true, singular primitive fields, repeated fields, , map fields serialized. if false, serialize non-empty fields. singular message fields , oneof fields not affected option. preserving_proto_field_name: if true, use original proto field names defined in .proto file. if false, convert field names lowercamelcase. returns: string containing json formatted protocol buffer message.
Comments
Post a Comment