类型错误:<;0x7f81898af438处的botocore.response.StreamingBody对象>;JSON不可序列化

类型错误:<;0x7f81898af438处的botocore.response.StreamingBody对象>;JSON不可序列化,json,amazon-web-services,python-3.5,boto3,amazon-polly,Json,Amazon Web Services,Python 3.5,Boto3,Amazon Polly,我正在尝试使用Python创建一个AWS Lambda函数,它将从一些API接收一个文本,并返回一个包含AudioStream对象的JSON。为此,我使用了AWS Polly。目前,我能够在我的机器上从AWS获得音频流,并且工作正常 为了使用AWS Polly,我创建了一个特殊用户,并为他提供了AmazonPollyReadOnlyAccess和AmazonPollyFullAccessaccess import boto3,json polly= boto3.client('polly')

我正在尝试使用Python创建一个AWS Lambda函数,它将从一些API接收一个文本,并返回一个包含
AudioStream
对象的JSON。为此,我使用了AWS Polly。目前,我能够在我的机器上从AWS获得
音频流
,并且工作正常

为了使用AWS Polly,我创建了一个特殊用户,并为他提供了
AmazonPollyReadOnlyAccess
AmazonPollyFullAccess
access

import boto3,json

polly= boto3.client('polly')
text='<speak>hey there wassup </speak>'

spoken_text=polly.synthesize_speech(Text=text,OutputFormat='mp3',VoiceId='Aditi',TextType='ssml')

newdata=json.dumps(spoken_text)
# return newdata
print(type(spoken_text))
导入boto3,json
polly=boto3.client('polly')
text='嘿,这里有个sup'
口语\u text=polly.synthesis\u语音(text=text,OutputFormat='mp3',VoiceId='Aditi',TextType='ssml'))
newdata=json.dumps(口语文本)
#返回新数据
打印(键入(口语文本))
但是当我试图使用这个代码片段以JSON格式返回响应时,我得到了错误

/usr/bin/python3.5 /talker/aditi.py
Traceback (most recent call last):
  File "/talker/aditi.py", line 9, in <module>
    newdata=json.dumps(spoken_text)
  File "/usr/lib/python3.5/json/__init__.py", line 230, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.5/json/encoder.py", line 198, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.5/json/encoder.py", line 256, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.5/json/encoder.py", line 179, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <botocore.response.StreamingBody object at 0x7f81898af438> is not JSON serializable

Process finished with exit code 1
/usr/bin/python3.5/talker/aditi.py
回溯(最近一次呼叫最后一次):
文件“/talker/aditi.py”,第9行,在
newdata=json.dumps(口语文本)
文件“/usr/lib/python3.5/json/_init__.py”,第230行,转储
返回默认编码器编码(obj)
文件“/usr/lib/python3.5/json/encoder.py”,第198行,在encode中
chunks=self.iterencode(o,\u one\u shot=True)
iterencode中的文件“/usr/lib/python3.5/json/encoder.py”,第256行
返回_iterencode(o,0)
默认情况下,文件“/usr/lib/python3.5/json/encoder.py”第179行
raise TypeError(repr(o)+“不可JSON序列化”)
TypeError:不可序列化JSON
进程已完成,退出代码为1

如何正确执行此操作?

您需要使用以下函数传递它:

newdata=bytes(json.dumps(spoken_text))
当您要读取返回值时,需要使用以下方法:

print(returned_value.read())
副本