Python |如何从 AWS 响应的结果中解析 JSON?
Python | How to parse JSON from results from AWS response?
我试图获取 VersionLabel
的值,即 php-v1
但我的代码无法正常工作,我不知道我做错了什么。
你能告诉我哪里出了问题吗?我该如何解析 php-v1
?
这是我的错误信息。
TypeError: the JSON object must be str, not 'dict'
这是我的代码。
#!/usr/bin/env python3
import boto3
import json
def get_label():
try:
env_name = 'my-env'
eb = boto3.client('elasticbeanstalk')
response = eb.describe_instances_health(
EnvironmentName=env_name,
AttributeNames=[
'Deployment'
]
)
#print(response)
data = json.loads(response)
print(data['VersionLabel'])
except:
raise
if __name__ == '__main__':
get_label()
这是调用 print(response)
时我从 AWS 得到的响应。
{
'InstanceHealthList':[
{
'InstanceId':'i-12345678',
'Deployment':{
'DeploymentId':2,
'DeploymentTime':datetime.datetime(2016,
9,
29,
4,
29,
26,
tzinfo=tzutc()),
'Status':'Deployed',
'VersionLabel':'php-v1'
}
}
],
'ResponseMetadata':{
'HTTPStatusCode':200,
'RequestId':'12345678-1234-1234-1234-123456789012',
'RetryAttempts':0,
'HTTPHeaders':{
'content-length':'665',
'content-type':'text/xml',
'date':'Sat, 01 Oct 2016 11:04:56 GMT',
'x-amzn-requestid':'12345678-1234-1234-1234-123456789012'
}
}
}
非常感谢!
根据 boto3 文档 [http://boto3.readthedocs.io/en/latest/reference/services/elasticbeanstalk.html?highlight=describe_instances_health#ElasticBeanstalk.Client.describe_instances_health],describe_instances_health 方法 returns dict 而不是 json。因此,您无需进行转换。
要从数据中获取 VersionLabel,请使用 -
data ['InstanceHealthList'][0]['Deployment']['VersionLabel']
编辑:请注意,上面从可能的多个实例中获取第一个实例的 VersionLabel。如果您有多个实例并且它们恰好具有不同的 VersionLabel 值,那么您将需要额外的逻辑来获取您需要的那个。
以防万一要求将 boto 响应转换为合法的 json 格式 -
import json
response_json = json.dumps(response, default=str))
datetime.datetime 需要在 dict 到 json 转换期间处理
我试图获取 VersionLabel
的值,即 php-v1
但我的代码无法正常工作,我不知道我做错了什么。
你能告诉我哪里出了问题吗?我该如何解析 php-v1
?
这是我的错误信息。
TypeError: the JSON object must be str, not 'dict'
这是我的代码。
#!/usr/bin/env python3
import boto3
import json
def get_label():
try:
env_name = 'my-env'
eb = boto3.client('elasticbeanstalk')
response = eb.describe_instances_health(
EnvironmentName=env_name,
AttributeNames=[
'Deployment'
]
)
#print(response)
data = json.loads(response)
print(data['VersionLabel'])
except:
raise
if __name__ == '__main__':
get_label()
这是调用 print(response)
时我从 AWS 得到的响应。
{
'InstanceHealthList':[
{
'InstanceId':'i-12345678',
'Deployment':{
'DeploymentId':2,
'DeploymentTime':datetime.datetime(2016,
9,
29,
4,
29,
26,
tzinfo=tzutc()),
'Status':'Deployed',
'VersionLabel':'php-v1'
}
}
],
'ResponseMetadata':{
'HTTPStatusCode':200,
'RequestId':'12345678-1234-1234-1234-123456789012',
'RetryAttempts':0,
'HTTPHeaders':{
'content-length':'665',
'content-type':'text/xml',
'date':'Sat, 01 Oct 2016 11:04:56 GMT',
'x-amzn-requestid':'12345678-1234-1234-1234-123456789012'
}
}
}
非常感谢!
根据 boto3 文档 [http://boto3.readthedocs.io/en/latest/reference/services/elasticbeanstalk.html?highlight=describe_instances_health#ElasticBeanstalk.Client.describe_instances_health],describe_instances_health 方法 returns dict 而不是 json。因此,您无需进行转换。 要从数据中获取 VersionLabel,请使用 -
data ['InstanceHealthList'][0]['Deployment']['VersionLabel']
编辑:请注意,上面从可能的多个实例中获取第一个实例的 VersionLabel。如果您有多个实例并且它们恰好具有不同的 VersionLabel 值,那么您将需要额外的逻辑来获取您需要的那个。
以防万一要求将 boto 响应转换为合法的 json 格式 -
import json
response_json = json.dumps(response, default=str))
datetime.datetime 需要在 dict 到 json 转换期间处理