Convert None to empty string automatically

disk_details_list = list(disk_details.values('id', 'date'))

Sometimes date is not available and hence None

print("disk_details_list = ", disk_details_list) prints :

disk_details_list =  [{'id': 102, 'date': None }]


disk_details_list =  [{'id': 102, 'date': datetime.datetime(2020, 6, 9, 12, 42, 15, tzinfo=datetime.timezone.utc) }]

The problem is when I try to do a json.dumps of the list :
disk_details_json = json.dumps(disk_details_list)

Object of type datetime is not JSON serializable

How do I overcome this None part ?

If you are asking to solve this error than it is not because of the None value in date, it’s because json.dumps cannot convert datetime.datetime() object. You have to convert it into string like"%Y-%m-%d %H:%M:%S") this.

1 Like

you can try json.dumps(yourList, default=str)

1 Like