Попробуйте,
import yaml
import json
with open("./test.yaml", 'r') as yaml_in, open("yml_test.json", "w") as json_out:
data = json.loads(json.dumps(yaml.load(yaml_in, Loader=yaml.FullLoader)))
output_dict = {}
for url in data["paths"]:
for method in data["paths"][url]:
output_dict[url + "/" + method] = {}
output_dict[url + "/" + method]["parameters"] = {}
if "consumes" in data["paths"][url][method]:
output_dict[url+"/"+method]["consumes"] = data["paths"][url][method]["consumes"]
if "produces" in data["paths"][url][method]:
output_dict[url+"/"+method]["produces"] = data["paths"][url][method]["produces"]
if data["paths"][url][method]["parameters"]:
if "in" in data["paths"][url][method]["parameters"][0]:
output_dict[url+"/"+method]["parameters"]["in"] = data["paths"][url][method]["parameters"][0]["in"]
if "name" in data["paths"][url][method]["parameters"][0]:
output_dict[url+"/"+method]["parameters"]["name"] = data["paths"][url][method]["parameters"][0]["name"]
if "required" in data["paths"][url][method]["parameters"][0]:
output_dict[url+"/"+method]["parameters"]["required"] = data["paths"][url][method]["parameters"][0]["required"]
if "type" in data["paths"][url][method]["parameters"][0]:
output_dict[url+"/"+method]["parameters"]["type"] = data["paths"][url][method]["parameters"][0]["type"]
json.dump(output_dict, json_out)
OutPut отображается ниже и сохраняет то же самое в yml_test.json
файле.
{'/pet/post': {'parameters': {'in': 'body', 'name': 'body', 'required': True}, 'consumes': ['application/json', 'application/xml'], 'produces': ['application/xml', 'application/json']}, '/pet/put': {'parameters': {'in': 'body', 'name': 'body', 'required': True}, 'consumes': ['application/json', 'application/xml'], 'produces': ['application/xml', 'application/json']}, '/pet/findByStatus/get': {'parameters': {'in': 'query', 'name': 'status', 'required': True, 'type': 'array'}, 'produces': ['application/xml', 'application/json']}, '/pet/findByTags/get': {'parameters': {'in': 'query', 'name': 'tags', 'required': True, 'type': 'array'}, 'produces': ['application/xml', 'application/json']}, '/pet/{petId}/get': {'parameters': {'in': 'path', 'name': 'petId', 'required': True, 'type': 'integer'}, 'produces': ['application/xml', 'application/json']}, '/pet/{petId}/post': {'parameters': {'in': 'path', 'name': 'petId', 'required': True, 'type': 'integer'}, 'consumes': ['application/x-www-form-urlencoded'], 'produces': ['application/xml', 'application/json']}, '/pet/{petId}/delete': {'parameters': {'in': 'header', 'name': 'api_key', 'required': False, 'type': 'string'}, 'produces': ['application/xml', 'application/json']}, '/pet/{petId}/uploadImage/post': {'parameters': {'in': 'path', 'name': 'petId', 'required': True, 'type': 'integer'}, 'consumes': ['multipart/form-data'], 'produces': ['application/json']}, '/store/inventory/get': {'parameters': {}, 'produces': ['application/json']}, '/store/order/post': {'parameters': {'in': 'body', 'name': 'body', 'required': True}, 'produces': ['application/xml', 'application/json']}, '/store/order/{orderId}/get': {'parameters': {'in': 'path', 'name': 'orderId', 'required': True, 'type': 'integer'}, 'produces': ['application/xml', 'application/json']}, '/store/order/{orderId}/delete': {'parameters': {'in': 'path', 'name': 'orderId', 'required': True, 'type': 'integer'}, 'produces': ['application/xml', 'application/json']}, '/user/post': {'parameters': {'in': 'body', 'name': 'body', 'required': True}, 'produces': ['application/xml', 'application/json']}, '/user/createWithArray/post': {'parameters': {'in': 'body', 'name': 'body', 'required': True}, 'produces': ['application/xml', 'application/json']}, '/user/createWithList/post': {'parameters': {'in': 'body', 'name': 'body', 'required': True}, 'produces': ['application/xml', 'application/json']}, '/user/login/get': {'parameters': {'in': 'query', 'name': 'username', 'required': True, 'type': 'string'}, 'produces': ['application/xml', 'application/json']}, '/user/logout/get': {'parameters': {}, 'produces': ['application/xml', 'application/json']}, '/user/{username}/get': {'parameters': {'in': 'path', 'name': 'username', 'required': True, 'type': 'string'}, 'produces': ['application/xml', 'application/json']}, '/user/{username}/put': {'parameters': {'in': 'path', 'name': 'username', 'required': True, 'type': 'string'}, 'produces': ['application/xml', 'application/json']}, '/user/{username}/delete': {'parameters': {'in': 'path', 'name': 'username', 'required': True, 'type': 'string'}, 'produces': ['application/xml', 'application/json']}}