0

I have a list of dictionaries (each dictionary representing 1 record of an SQL query) which I am trying to serialize as JSON. There are several datetime.date and datetime.datetime objects in these dictionaries which are proving difficult to serialize. Initially, the error message is:

TypeError: datetime.date(2012, 5, 23) is not JSON serializable

After adding a handler to the json.dumps call the error message is:

TypeError: date_handler() takes exactly 1 argument (2 given)

date_handler looks like this:

    def date_handler(obj):
        return obj.isoformat() if hasattr(obj, 'isoformat') else obj

I'm running this as part of a Trac plugin however I think this is just an isolated python issue but have no idea what to do to sort it - does anyone have any ideas?

1
  • 1
    stackoverflow.com/questions/455580 could provide some more insight/options, at least for preliminary solutions. There are critical comments related to timezone support.
    – hasienda
    Commented Jul 17, 2012 at 21:42

2 Answers 2

2

I solved this by applying .isoformat() fields that were dates or timestamps in the dictionary as I'm importing the data from a database and so know what the field types are beforehand, thanks for your help!

0

See a solution from the Django domain.

Not the answer you're looking for? Browse other questions tagged or ask your own question.