'Unformatted date time in response from step function execution history

I am calling below method to get the execution's history of a step function as mentioned in AWS Docs .

https://docs.aws.amazon.com/step-functions/latest/apireference/API_GetExecutionHistory.html

when i call same method passing the ARN i get response but with date time is not in correct format

client = boto3.client('stepfunctions')
response = client.get_execution_history(
executionArn=arn,
reverseOrder=True
)

I get the response like below

Part of response

{
   "events":[
      {
         "timestamp":datetime.datetime(2022,
         5,
         13,
         4,
         50,
         13,
         947000,
         "tzinfo=tzlocal())",
         "type":"ExecutionSucceeded",
         "id":49,
         "previousEventId":48,
         "executionSucceededEventDetails":{

and that's why when we try to import to step function it throws error

Can some one help how we can get valid json specially time stamp part on correct format ?



Solution 1:[1]

get_execution_history returns event timestamps as Python datetimes. Convert them to ISO strings with the isoformat method:

def with_iso_ts(e):
  ts = {"timestamp": e["timestamp"].isoformat()}
  e.update(ts)
  return e

events = [with_iso_ts(e) for e in response["events"]]

json_encoded = json.dumps(events)

Returns the expected string timestamp output:

[{'timestamp': '2022-04-22T16:13:49.241000+02:00',
  'type': 'ExecutionSucceeded',
  'id': 14,
  'previousEventId': 13,

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1