Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON-serializing non-string dictionary keys

Tags:

python

json

I know I can do:

import datetime

def default(o):
    if type(o) is datetime.datetime:
        return o.isoformat()

data = {'a': datetime.datetime.today()}
json.dumps(data, default=default)
# '{"a": "2015-01-22T01:04:23.121392"}'

That works perfect. Now what if I have datetimes as my keys:

data = {datetime.datetime.today(): 'a'}

If I try the same it fails with a:

TypeError: keys must be a string

Is there any way I can do something similar, i.e. a custom converter but for keys?

Note: This is just a simple example. I have a deep nested dict structure where some keys are not strings.

EDIT: A nested example, but note that I don't have control over the data structure, it comes from an external function:

data = {'a': {datetime.datetime.today(): '1'}}
like image 892
Elian Avatar asked Jan 22 '15 00:01

Elian


3 Answers

You could do this:

class DatesToStrings(json.JSONEncoder):
    def _encode(self, obj):
        if isinstance(obj, dict):
            def transform_date(o):
                return self._encode(o.isoformat() if isinstance(o, datetime) else o)
            return {transform_date(k): transform_date(v) for k, v in obj.items()}
        else:
            return obj

    def encode(self, obj):
        return super(DatesToStrings, self).encode(self._encode(obj))

>>> json.dumps({"a": {datetime.now(): 3}}, cls=DatesToStrings)
'{"a": {"2015-01-22T11:49:25.910261": 3}}'
like image 165
Andrew Magee Avatar answered Nov 06 '22 15:11

Andrew Magee


Upon Andrew Magee's and add nested list/set support:

import json
from datetime import datetime


class DatesToStrings(json.JSONEncoder):
    def _encode(self, obj):
        def transform_date(o):
            return self._encode(o.isoformat() if isinstance(o, datetime) else o)
        if isinstance(obj, dict):
            return {transform_date(k): transform_date(v) for k, v in obj.items()}
        elif isinstance(obj, list) or isinstance(obj, set):
            return [transform_date(l) for l in obj]
        else:
            return obj

    def encode(self, obj):
        return super(DatesToStrings, self).encode(self._encode(obj))


print(json.dumps({"a": datetime.now()}, cls=DatesToStrings))
print(json.dumps({datetime.now(): 1}, cls=DatesToStrings))
print(json.dumps({"a": {datetime.now(): 3}}, cls=DatesToStrings))
print(json.dumps({"a": [datetime.now()]}, cls=DatesToStrings))
print(json.dumps({"a": {1: [datetime.now()]}}, cls=DatesToStrings))
print(json.dumps({"a": [{1: [datetime.now()]}]}, cls=DatesToStrings))
print(json.dumps({"a": {datetime.now()}}, cls=DatesToStrings))
like image 25
Sola Yang Avatar answered Nov 06 '22 15:11

Sola Yang


Here is the recursive version - note that I won't guarantee it will be any faster than the pickled version:

def dictRecursiveFormat(d): 
        for key, val in list(d.items()):
            if isinstance(key, datetime.datetime): 
                val = d.pop(key)
                d[str(key)] = val 
            if isinstance(val, datetime.datetime) and isinstance(key, datetime.datetime): 
                d[str(key)] = str(val)
            elif isinstance(val, datetime.datetime):
                d[key] = str(val)
            if type(val) is dict: 
                dictRecursiveFormat(val)

example:

In [52]: d= {'a': datetime.datetime.now(), 'b': {datetime.datetime.now(): datetime.datetime.now()}}

In [53]: dictRecursiveFormat(d)

In [54]: d
Out[54]: 
{'a': '2015-01-21 19:33:52.293182',
 'b': {'2015-01-21 19:33:52.293240': '2015-01-21 19:33:52.293229'}}
like image 5
user3467349 Avatar answered Nov 06 '22 15:11

user3467349