使用python解析并操作Postman Collection v2.1文件

from functools import partial
import json

t = 'D:/我的/chaos-exam.postman_collection.json'


def parse_deal(x: dict):
    count = 0
    if 'item' in x:
        count = count + sum(parse_deal(i) for i in x['item'])
    elif 'request' in x:
        count = count + 1
        if "auth" in x['request']:
            x['request'].pop("auth")
        if "response" in x:
            x['response'] = [j for j in x['response'] if j['name'] not in (
                "OK", "Created", "Unauthorized", "Forbidden", "Not Found")]
        # print(json.dumps(x, indent=4))
    return count


openx = partial(open, encoding='utf8')
with openx(t, 'r') as fp:
    json_data = json.load(fp)
    count = parse_deal(json_data)
print("接口数量:", count)
with openx(t, 'w') as fp:
    json.dump(json_data, fp, indent=4)

posted @ 2022-06-02 15:51  豆苗稀  阅读(143)  评论(0编辑  收藏  举报