Runtime.MarshalError in python

Question:

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
Asked By: Ani

||

Answers:

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.

Answered By: Joseph Lane

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)
Answered By: Marcin

I was struggling with this today with a method that also returns a datetime.

In my example 'JoinedTimestamp': datetime(2015, 1, 1) resulting in the same Unable to marshal response.

If you don’t need the CreatedTime value you might as well remove it from the response as:

    for account in list_accounts_response["Accounts"]:
        if "JoinedTimestamp" in account:
            del account["JoinedTimestamp"]

To follow up on Joseph Lane’s answer, transforming this value could be something along the lines of:

    for account in list_accounts_response["Accounts"]:
        if "JoinedTimestamp" in account:
            account["JoinedTimestamp"] = str(account["JoinedTimestamp"])
Answered By: Jonas Mellquist