Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

2
  • Monkey patch FTW. The nasty thing is of course that this modifies the behaviour of the json module in your whole application, which may surprise others in a large application, so should generally be used with care imho. Commented Feb 10, 2019 at 19:16
  • Note that this solution will only work for datetime objects. Dates will be encoded as null. To fix this, the code can be changed to: json.JSONEncoder.default = lambda self, obj: (obj.isoformat() if isinstance(obj, datetime.date) else None) Commented Apr 6 at 13:13