Python | How to parse JSON from results from AWS response?

Multi tool use
Python | How to parse JSON from results from AWS response?
I was trying to get the value of VersionLabel
which is php-v1
but my code doesn't work properly and I don't know what I'm doing wrong.
VersionLabel
php-v1
Could you please let me know what's wrong and how can I parse the php-v1
?
php-v1
This is my error message.
TypeError: the JSON object must be str, not 'dict'
TypeError: the JSON object must be str, not 'dict'
This is my code.
#!/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()
This is the response I got from AWS when print(response)
is invoked.
print(response)
{
'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'
}
}
}
Thanks so much!
dict
response
If your
print(response)
is saying such things as datetime.datetime
and using single quotes (illegal in JSON), it definitely converted it for you already.– Nick T
Oct 1 '16 at 16:07
print(response)
datetime.datetime
Try commenting out
#data = json.loads(response)
and add this line instead: print(response['InstanceHealthList'][0]['Deployment']['VersionLabel'])
. Does this give you the desired result?– B B
Oct 1 '16 at 16:14
#data = json.loads(response)
print(response['InstanceHealthList'][0]['Deployment']['VersionLabel'])
@tdelaney Thanks for checking! So meaning I have to stop using json module? How can I get the value of the VersionLabel?
– sedawkgrep
Oct 1 '16 at 16:15
@BB 's suggestion should do it.
– tdelaney
Oct 1 '16 at 16:18
2 Answers
2
As per the boto3 docs [http://boto3.readthedocs.io/en/latest/reference/services/elasticbeanstalk.html?highlight=describe_instances_health#ElasticBeanstalk.Client.describe_instances_health], the describe_instances_health method returns dict and not json. Hence, there is no need for you to do the conversion.
To get VersionLabel from data, use -
data ['InstanceHealthList'][0]['Deployment']['VersionLabel']
Edit : Note that the above fetches the VersionLabel for the first instance, out of possible multiple instances. In case you have multiple instances and they happen to have different values of VersionLabel, then you would require additional logic to get the one you need.
As boto3 already give the response in dictionary, You can use pprint or use an "IDE evaluator". To see the drop down of the response.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I'm not a boto3 user but it appears that the json decode has already been done for you. Does the
dict
inresponse
look right to you?– tdelaney
Oct 1 '16 at 15:59