Python | How to parse JSON from results from AWS response?
<p>I was trying to get the value of <code>VersionLabel</code> which is <code>php-v1</code> but my code doesn't work properly and I don't know what I'm doing wrong.</p>

<p>Could you please let me know what's wrong and how can I parse the <code>php-v1</code>?</p>

<p>This is my error message.</p>

<p><code>TypeError: the JSON object must be str, not 'dict'</code></p>

<p>This is my code.</p>

<pre><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()
</code></pre>

<p>This is the response I got from AWS when <code>print(response)</code> is invoked.</p>

<pre><code>{
   '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'
      }
   }
}
</code></pre>

<p>Thanks so much!</p>

<p>As per the boto3 docs [<a href="http://boto3.readthedocs.io/en/latest/reference/services/elasticbeanstalk.html?highlight=describe_instances_health#ElasticBeanstalk.Client.describe_instances_health]" rel="nofollow">http://boto3.readthedocs.io/en/latest/reference/services/elasticbeanstalk.html?highlight=describe_instances_health#ElasticBeanstalk.Client.describe_instances_health]</a>, 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 - </p>

<pre><code>data ['InstanceHealthList'][0]['Deployment']['VersionLabel']
</code></pre>

<p>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.</p>

python json amazon-web-services boto