Building a Generic API Diff Tool for System Refactoring and Data Migration Testing

This article describes how a Python‑based diff utility was created to compare old and new API responses during a promotion change‑record migration, detailing its implementation, handling of JSON structures, improvements over the first version, and plans for integration with an API testing platform.

转转QA
转转QA
转转QA
Building a Generic API Diff Tool for System Refactoring and Data Migration Testing

When faced with a system refactor that required migrating promotion change‑record data to a new database, the author needed a reliable way to verify that both new operation logs and cleaned‑up legacy data were accurate. By comparing the old and new API responses, they realized a diff approach could quickly validate data consistency.

The tool was built using Python's requests.get(url,cookies,params) to fetch JSON responses from the old and new endpoints. It extracts key fields such as createTime, operateMsg, and operateChange, matches timestamps, and uses regular expressions to compare price changes, handling unit differences between "cents" and "yuan".

Initial implementation relied on string split operations, which proved fragile. The author switched to json.loads to parse responses into dictionaries, allowing direct key‑value comparisons. A recursive function navigates nested structures (e.g., respData → commentList → name) to locate target fields.

Two major limitations of the first version were addressed: (1) the need to specify the full hierarchy of fields for matching, and (2) the inability of simple "==" comparison to handle unordered lists. The solution introduced a parameter for field paths and used isinstance(..., list) to perform element‑wise containment checks, making the diff tolerant to list order variations.

The second version also supports a mode where no specific field list is provided; it recursively compares all keys and values of the two JSON objects, using dict.keys() and length checks to determine overall equality.

Finally, the Python code was wrapped into an HTTP interface, exposing a generic diff service. Users can submit a JSON payload containing two JSON objects and an optional diff_word to focus the comparison on particular keywords, or omit it to compare the entire payloads.

Future work includes integrating this diff capability into the team's API testing platform, refining keyword formats for better usability, and ensuring the solution scales with broader testing scenarios.

data migrationPythonBackend DevelopmentJSONAPI testingdiff tool
转转QA
Written by

转转QA

In the era of knowledge sharing, discover 转转QA from a new perspective.

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.