Using OrderedDict and json.dumps Separators to Generate Consistent MD5 Signatures for Python ERP API Integration

The article explains how unordered Python dictionaries cause inconsistent MD5 signatures when assembling JSON payloads for an ERP API, and demonstrates using collections.OrderedDict together with json.dumps separators to produce a stable, correctly signed request.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Using OrderedDict and json.dumps Separators to Generate Consistent MD5 Signatures for Python ERP API Integration

When integrating a Python application with the Guanyiyun ERP system, the required request payload includes a signature that is computed by concatenating a secret string before and after the JSON representation of the data and then applying an MD5 hash (uppercase 32‑character). The original PHP example works because the associative array preserves order, but Python's built‑in dict is unordered, causing the JSON string (and thus the MD5) to change on each run.

The solution is to use collections.OrderedDict to keep the key order consistent and to call json.dumps with the separators=(',' , ':') argument, which removes the space after commas that would otherwise alter the hash.

Example code:

def getShops():
    data = OrderedDict()
    data["appkey"] = appkey
    data["sessionkey"] = sessionkey
    data["method"] = method
    data["page_no"] = "1"
    data["page_size"] = "10"
    data["sign"] = sign(data, secret)
    response = requests.post(url=url, data=json.dumps(data))
    print(response.text)

def sign(data, secret):
    json_str = json.dumps(data, separators=(",", ":"))
    full_str = secret + json_str + secret
    sign_code = hashlib.md5(full_str.encode("utf-8")).hexdigest().upper()
    return sign_code

By assembling the payload with an OrderedDict and serializing it without extra spaces, the generated MD5 signature matches the ERP's expectations, eliminating the frequent errors previously encountered.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonJSONMD5signatureordered dict
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.