你能验证每个对象 json 吗?
Can you validate json per object?
我想根据定义的 json 架构验证我的 json 数据。
我有以下脚本:
import json
import jsonschema
from jsonschema import validate
schema = {
"type": "array",
"items": {
"properties": {
"description": {
"type": "string"
},
"status": {
"type": "boolean"
},
"value1": {
"type": "number"
},
"value2": {
"type": "number"
}
}
}
}
data = json.loads('[{"description": "Hello world!", "status": true, "value1": 1, "value2": 2}, {"description": "Test", "status": "true", "value1": 3, "value2": 4}]')
def validateJson(data):
try:
validate(instance=data, schema=schema)
except jsonschema.exceptions.ValidationError as err:
print(err)
return False
return True
# validate it
isValid = validateJson(data)
if isValid:
print(data)
print("Given JSON data is Valid")
else:
print(data)
print("Given JSON data is InValid")
当我 运行 代码时,我发现数据无效,因为第二个对象中的“true”不是布尔值。但是正如你所看到的,第一个对象
{"description": "Hello world!", "status": true, "value1": 1, "value2": 2}
有效。所以我真正想要的是 validateJSON 函数给我有效和无效的对象,而不是验证整个数据。我该怎么做?
定义一个架构,您的 array
中的每个 JSON object
都应该根据该架构进行验证,并独立验证每个架构。
schema = {
"type": "object",
"properties": {
"description": {
"type": "string"
},
"status": {
"type": "boolean"
},
"value1": {
"type": "number"
},
"value2": {
"type": "number"
}
}
}
然后:
>>> for i, obj in enumerate(data):
... try:
... validate(instance=obj, schema=schema)
... except Exception as e:
... print(f"obj {i} invalid: {e}")
... else:
... print(f"obj {i} valid")
obj 0 valid
obj 1 invalid: 'true' is not of type 'boolean'
Failed validating 'type' in schema['properties']['status']:
{'type': 'boolean'}
On instance['status']:
'true'
如果您想保留对有效对象和无效对象的引用,您可以将它们的数字索引附加到两个列表,例如 valid_idxs: list[int]
和 invalid_idxs: list[int]
。
我想根据定义的 json 架构验证我的 json 数据。 我有以下脚本:
import json
import jsonschema
from jsonschema import validate
schema = {
"type": "array",
"items": {
"properties": {
"description": {
"type": "string"
},
"status": {
"type": "boolean"
},
"value1": {
"type": "number"
},
"value2": {
"type": "number"
}
}
}
}
data = json.loads('[{"description": "Hello world!", "status": true, "value1": 1, "value2": 2}, {"description": "Test", "status": "true", "value1": 3, "value2": 4}]')
def validateJson(data):
try:
validate(instance=data, schema=schema)
except jsonschema.exceptions.ValidationError as err:
print(err)
return False
return True
# validate it
isValid = validateJson(data)
if isValid:
print(data)
print("Given JSON data is Valid")
else:
print(data)
print("Given JSON data is InValid")
当我 运行 代码时,我发现数据无效,因为第二个对象中的“true”不是布尔值。但是正如你所看到的,第一个对象
{"description": "Hello world!", "status": true, "value1": 1, "value2": 2}
有效。所以我真正想要的是 validateJSON 函数给我有效和无效的对象,而不是验证整个数据。我该怎么做?
定义一个架构,您的 array
中的每个 JSON object
都应该根据该架构进行验证,并独立验证每个架构。
schema = {
"type": "object",
"properties": {
"description": {
"type": "string"
},
"status": {
"type": "boolean"
},
"value1": {
"type": "number"
},
"value2": {
"type": "number"
}
}
}
然后:
>>> for i, obj in enumerate(data):
... try:
... validate(instance=obj, schema=schema)
... except Exception as e:
... print(f"obj {i} invalid: {e}")
... else:
... print(f"obj {i} valid")
obj 0 valid
obj 1 invalid: 'true' is not of type 'boolean'
Failed validating 'type' in schema['properties']['status']:
{'type': 'boolean'}
On instance['status']:
'true'
如果您想保留对有效对象和无效对象的引用,您可以将它们的数字索引附加到两个列表,例如 valid_idxs: list[int]
和 invalid_idxs: list[int]
。