Convert python dictionaries with dates into JSON format

Python dictionaries are similar to json format, but they are not JSON. To convert dictionaries to json you'll need to import json module in python.

Consider the following dictionary

# this example is taken from AWS S3 with boto3 library
my_dictionary = {
    'Contents': [
        {
            'Key': 'filename-1.pdf',
            'LastModified': datetime.datetime(2018, 12, 19, 15, 32, 54, tzinfo=tzutc()),
            'Size': 1545033
        },
        {
            'Key': 'filename-2.pdf',
            'LastModified': datetime.datetime(2018, 12, 19, 15, 32, 54, tzinfo=tzutc()),
            'Size': 4222827
        },
        {
            'Key': 'filename-3.pdf',
            'LastModified': datetime.datetime(2018, 12, 19, 15, 32, 54, tzinfo=tzutc()),
            'Size': 640925
        },
        {
            'Key': 'filename-4.pdf',
            'LastModified': datetime.datetime(2018, 12, 19, 15, 32, 54, tzinfo=tzutc()),
            'Size': 1617724
        },
    ]
}

To convert any dictionary to json, first you need to import json module:

import json

Convert simple dictionary into JSON format

Then use json.dumps method:

Note that the following example will output an error if the my_dictionary is provided
import json

simple_dictionary = {
    'Key': 'filename.pdf',
    'Size': 123123
}

data_as_json = json.dumps(simple_dictionary)

print(data_as_json)

The result will be:

{"Key": "filename.pdf", "Size": 123123}

Convert dictionary with datetime in json format

In the above example, if you try to convert the first dictionary that contains datetime object it will output the following error:

TypeError: Object of type datetime is not JSON serializable

To fix this problem we can use a helper function to convert the datetime object into seconds as follows:

import datetime
from dateutil.tz import tzutc
import json

my_dictionary = {
    'Contents': [
        {
            'Key': 'filename-1.pdf',
            'LastModified': datetime.datetime(2018, 12, 19, 15, 32, 54, tzinfo=tzutc()),
            'Size': 1545033
        },
        {
            'Key': 'filename-2.pdf',
            'LastModified': datetime.datetime(2018, 12, 19, 15, 32, 54, tzinfo=tzutc()),
            'Size': 4222827
        },
        {
            'Key': 'filename-3.pdf',
            'LastModified': datetime.datetime(2018, 12, 19, 15, 32, 54, tzinfo=tzutc()),
            'Size': 640925
        },
        {
            'Key': 'filename-4.pdf',
            'LastModified': datetime.datetime(2018, 12, 19, 15, 32, 54, tzinfo=tzutc()),
            'Size': 1617724
        },
    ]
}


def convert_timestamp(item_date_object):
    if isinstance(item_date_object, (datetime.date, datetime.datetime)):
        return item_date_object.timestamp()


my_json_data = json.dumps(my_dictionary, default=convert_timestamp)

print(my_json_data)