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?

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

2 Answers2

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!

Gowri
  • 16,587
  • 26
  • 100
  • 160
pwilding
  • 33
  • 1
  • 6
0

See a solution from the Django domain.

hasienda
  • 2,390
  • 1
  • 13
  • 16