1 回答
TA贡献1827条经验 获得超8个赞
这是我很久以前用于类似目的的东西,希望你会发现它有帮助,或者至少是解决你的问题的良好起点!
import json
# Define default values to use for each type, adjust as needed
default_values = {
'string': '',
'int': 0,
'integer': 0,
'number': 0,
'array': [],
'list': [],
'tuple': [],
'dict': {},
'object': {},
'boolean': 'false',
}
# Iterate the schema and return simplified dictionary or JSON string
def schema_to_json(s, to_json=False):
res = {k: default_values[v['type']] for k, v in s.items()}
return json.dumps(res) if to_json else res
# Pass your schema to function and get simplified version back
schema = {
'name': {'type': 'string'},
'age': {'type': 'integer'},
}
py_dict = schema_to_json(schema) # Will return Python dictionary
json_string = schema_to_json(schema, True) # Will return JSON string
添加回答
举报