Runtime.MarshalError in python

Viewed 8893

I am Getting this error. I am executing code of aws lambda function using python 3.7 to know quicksight dashboard version. Thanks in advance!

errorMessage: "Unable to marshal response: Object of type datetime is not JSON serializable",

errorType : "Runtime.MarshalError"

Code-

import boto3
import time
import sys
client = boto3.client('quicksight')
def lambda_handler(event, context):
    response = client.list_dashboard_versions(AwsAccountId='11111', DashboardId='2222',MaxResults=10)
    return response
2 Answers

I quick fix could be:

import boto3
import time
import sys

import json

client = boto3.client('quicksight')

def lambda_handler(event, context):
    response = client.list_dashboard_versions(AwsAccountId='11111', DashboardId='2222',MaxResults=10)

    return json.dumps(response, default=str)

Looking at https://boto3.amazonaws.com/v1/documentation/api/1.14.8/reference/services/quicksight.html#QuickSight.Client.list_dashboard_versions the return looks like this -

{
    'DashboardVersionSummaryList': [
        {
            'Arn': 'string',
            'CreatedTime': datetime(2015, 1, 1),
            'VersionNumber': 123,
            'Status': 'CREATION_IN_PROGRESS'|'CREATION_SUCCESSFUL'|'CREATION_FAILED'|'UPDATE_IN_PROGRESS'|'UPDATE_SUCCESSFUL'|'UPDATE_FAILED',
            'SourceEntityArn': 'string',
            'Description': 'string'
        },
    ],
    'NextToken': 'string',
    'Status': 123,
    'RequestId': 'string'
}

As you can see, CreatedTime is returned as datetime. If you want to return this as a JSON, you should transform this value.

Related