要比较两个 JSON 模式,可以使用以下解决方法:
以下是一个示例代码:
import jsonschema
def compare_json_schemas(schema1, schema2):
try:
jsonschema.Draft7Validator.check_schema(schema1)
jsonschema.Draft7Validator.check_schema(schema2)
return schema1 == schema2
except jsonschema.exceptions.SchemaError as e:
print("Invalid JSON schema:", e)
return False
# Example usage
schema1 = {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"}
}
}
schema2 = {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"}
}
}
result = compare_json_schemas(schema1, schema2)
print(result) # Output: True
以下是一个示例代码:
import jsondiff
def compare_json_schemas(schema1, schema2):
diff = jsondiff.diff(schema1, schema2)
return diff
# Example usage
schema1 = {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"}
}
}
schema2 = {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
"address": {"type": "string"}
}
}
result = compare_json_schemas(schema1, schema2)
print(result)
这将输出两个 JSON 模式之间的差异。你可以根据需要自定义输出格式。