Comparing JSON Responses Between Old and New Services Using Python
This article explains how to compare JSON data returned by old and new services during a migration, demonstrates Python scripts for JSON and list comparisons, and provides useful tools and code snippets for validating data consistency before a full rollout.
This guide shows how to perform data comparison when migrating services by running old and new versions in parallel, initially directing only 10% of traffic to the new service and using the results to verify correctness before a complete switch.
First, install the required dependency:
pip3 install json_toolsFor JSON responses, the following Python script reads a list of parameters from a text file, calls the old and new APIs, retrieves their JSON payloads, and uses json_tools.diff to highlight differences:
# encoding:utf-8
'''两个json对比
通过搜索replace查找哪些不一致
'''
import json
import requests
import json_tools
from urllib import parse
import sys
from conf import hosts, apis
#读取txt里的参数作为变量,因为数据对比是尽可能全的包含向上已经存在的数据
resultpath = '../参数.txt'
lines = []
with open(resultpath, 'r') as file_to_read:
while True:
line = file_to_read.readline()
if not line:
break
line = line.strip('\n')
lines.append(line)
def test_json():
for i in lines:
url_old = hosts["test"] + apis["API_old"]["path"] + i
res_old = requests.get(url_old, cookies=graycookei)
query_old = res_old.json()
url_new = hosts["test"] + apis["API_new"]["path"] + i
res_new = requests.get(url_new, cookies=graycookei)
query_new = res_new.json()
contrast1 = json_tools.diff(query_old["data"]["expressInfo"], query_new["data"]["expressInfo"]))
# 打印出两个接口返回的json不一致的数据
print(contrast1, url_new)
test_query_old()When the service returns data as a list, the script below demonstrates how to find common elements and identify items that are missing in either list using list comprehensions:
list1 = [1, 2.3, 3, 4, 5]
list2 = [2, 3.3, 4, 5]
c = [x for x in list1 if x in list2]
d = [y for y in (list1 + list2) if y not in c]
print(1, c)
print("不一样的数据或缺失的数据", d)For quick visual comparison of two JSON objects, an online tool such as https://www.sojson.com/compare.html can be used.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.