Convert String to Datetime Format

I receive data from a webhook that has a date that is formatted like this “2022-06-21T15:11:44Z”. Currently I just pipe that into a Mongo document but I want to see if I can reformat that into a datetime or ISO format so that it is not stored as a string in MongoDB. Is that something I can do with a custom expression or an additional step in the workflow between receiving the webhook and posting the data to MongoDb?

Realized I could use custom code, however I am using python – do I need to do some kind of async code for a simple string to date conversion? I am getting this warning:

Warning

Hi @kunalrye

That does sound like a bug, since Python code is synchronous by default.

Could you share the Python code you’ve written to manipulate the string here? That would help us reproduce and make a bug report if there is one.

@pierce

from pipedream.script_helpers import (steps, export)
import dateutil.parser as parser
# Reference data from previous steps
date_string = (steps["trigger"]["event"]["body"]["ReceivedAt"])
date = parser.parse(date_string)
# Return data for use in future steps
export("iso_date", {"date":date})

Hi @kunalrye

Thanks that explains it.

It looks like you’re parsing the date string from the HTTP request just fine, but the issue is the date is an instance of an object that is not JSON serializable:

TypeError: Object of type datetime is not JSON serializable

If you’re intending to format to an ISO string, try calling the .isoformat() method on the datetime instance:

export("iso_date", { "date": date.isoformat() })

I didn’t realize my workflow would error out because of this – is it possible to put this block in a try catch of sorts so that the flow continues?

Also it seems like even with .isoformat() it is still erroring out for some reason

Hi @kunalrye

Yes, any data being stored in a Data Store must be JSON serializable.

You can try a try…catch block, or return early out of the step if the data is missing.

If you could share a screenshot of the error or the full log text that would be helpful to further troubleshoot with you.