0
import datetime
import json
 def receive(self, event, **kwargs):
     payload={
              "id":event.id,
              "lastReceiveTime":event.lastreceivetime
              "createTime":event.createtime,
              "receiveTime":event.receivetime

            }
    r = requests.post("http://localhost:8000/curlRequest", json=payload, headers=self.headers)

    return event

i'm getting error Object of type datetime is not JSON serializable i have tried "lastReceiveTime":datetime.event.lastreceivetime....but this has also not worked.

Renu
  • 37
  • 8
  • what is your expected output – Dev Jun 09 '20 at 18:21
  • the output should be in json {"id": "f0ffedcd-c936-4cdf-addb-da19358fbd95", "lastReceiveTime":"2020-06-09T16:37:37.904Z","createTime": "2020-06-09T16:37:37.904Z","receiveTime":"2020-06-09T16:37:37.904Z"} – Renu Jun 09 '20 at 18:25
  • simple way you can convert the time into string like str(event.lastreceivetime) – Dev Jun 09 '20 at 18:38

1 Answers1

1

You could pass in the Unix time, and when you retrieve it in your application convert that to the format you prefer.

To get the Unix time in python:

import time
current_time = time.time()

current_time will be of type float.

In your case, you could also just convert the datetime object to a unix timestamp if you cannot control the type of event.createtime and event.recievetime.

The below code should do that:

import datetime
import time
import json
 def receive(self, event, **kwargs):
     last_time = event.lastrecievetime.timestamp()
     create_time = event.createtime.timestamp()
     recieve_time = event.recievetime.timestamp()
     payload={
              "id":event.id,
              "lastReceiveTime":last_time
              "createTime":create_time,
              "receiveTime":receive_time

            }
    r = requests.post("http://localhost:8000/curlRequest", json=payload, headers=self.headers)

    return event
Shreyas Sreenivas
  • 331
  • 1
  • 5
  • 12
  • You could convert the datetime to a unix timestamp using `[datetime obj].timestamp()` – Shreyas Sreenivas Jun 09 '20 at 18:46
  • Also note, when you recieve the data in your application at `http://localhost:8000/curlRequest`, make sure you convert the timestamp to the format your program requires. – Shreyas Sreenivas Jun 09 '20 at 19:09
  • i have followed this above code and in the output i got {"lastReceiveTime": 1591778442.753045, "createTime": 1591778442.738452, "receiveTime": 1591778442.753045} but we want date with time like this {lastReceiveTime":"2020-06-09T16:37:37.904Z","createTime": "2020-06-09T16:37:37.904Z","receiveTime":"2020-06-09T16:37:37.904Z"} – Renu Jun 10 '20 at 09:47
  • 1
    Yes, that is how unix time is represented. However if you want that particular format you could either convert it to a string in python and send it through JSON or convert the timestamp to a native datetime instance in your application (reccomended). Converting a timestamp to a Date object in JavaScript: https://stackoverflow.com/questions/847185/convert-a-unix-timestamp-to-time-in-javascript – Shreyas Sreenivas Jun 10 '20 at 09:52
  • No problem. If you have any further queries let me know – Shreyas Sreenivas Jun 10 '20 at 10:04