So I'm running into a problem with botocore and boto3 returning an invalid JSON payload.
The code i'm running:
import botocore.session
session = botocore.session.get_session()
client = session.create_client('ecs', region_name='us-east-1')
response = client.describe_services(
cluster='MyCluster',
services=[
'nodeweb'
]
)
Sample output:
{u'services': [{u'status': u'ACTIVE', u'taskDefinition': u'arn:aws:ecs:us-east-1:123456789012:task-definition/Webserver:36', u'pendingCount': 0, u'loadBalancers': [], u'placementConstraints': [], u'createdAt': datetime.datetime(2016, 12, 6, 12, 12, 38, 220000, tzinfo=tzlocal()), u'desiredCount': 1,
Lib's I have installed:
boto (2.42.0)
boto3 (1.4.4)
botocore (1.5.4)
It seems to be an issue with the following fields.
u'updatedAt': datetime.datetime(2017, 1, 20, 18, 31, 6, 82000, tzinfo=tzlocal()),
u'createdAt': datetime.datetime(2016, 12, 6, 12, 12, 38, 220000, tzinfo=tzlocal())
I tested the CLI version and it outputs the correct values.
aws ecs describe-services --cluster 'MyCluster' --services 'nodeweb'
Output:
"deployments": [
{
"status": "PRIMARY",
"pendingCount": 0,
"createdAt": 1484965866.082,
"desiredCount": 1,
"taskDefinition": "arn:aws:ecs:us-east-1:123456789012:task-definition/Webserver:36",
"updatedAt": 1484965866.082,
"id": "ecs-svc/123456789012",
"runningCount": 1
}
],
I hope I have provided enough information to help find out why this is happening.
Thanks to all!
Yeah so the reason that it is not valid JSON, is that python datetime.datetime's are not JSON serializeable. So you will have to add a handler to serialize the datetime to a string when dumping the JSON. Here is a Stack Overflow answer that shows how you can do that: http://stackoverflow.com/questions/35869985/datetime-datetime-is-not-json-serializable.
Hope this helps.
Thank you so much! That is what I needed to do. Sorry if it was incorrect for me to post here.
Most helpful comment
Yeah so the reason that it is not valid JSON, is that python
datetime.datetime's are not JSON serializeable. So you will have to add a handler to serialize the datetime to a string when dumping the JSON. Here is a Stack Overflow answer that shows how you can do that: http://stackoverflow.com/questions/35869985/datetime-datetime-is-not-json-serializable.Hope this helps.