Python 3 Generate MD5 for Dict Object

March 11, 2020
import hashlib
import json

item = {'name': 'Desmond', 'age': 40}

md5_hash = hashlib.md5(json.dumps(item, sort_keys=True).encode('utf-8')).hexdigest()
# 'b0aaa17f827b3a835865262b8ab2cf00'

NOTE: sort_keys=True is required else {'age': 40, 'name': 'Desmond'} will generate a different hash.

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.