'Validating JSON schema always return true
I've written a code that should validate my JSON schema by comparing the schema with the JSON string. It always returns True, even when instead of a given type in schema, I return a string or an int. I bet there is something that I missed in the logic of my schema to compare to. Here is a code:
import json
import jsonschema
from jsonschema import validate
Emp_Schema = {
"type": "object",
"outputs": {
"boundaries": {"type": "List[List[coords]]"},
"connecter": {"type": "List[bool]"},
"compare": {"type": "List[float]"},
"center": {"type": "List[List[coords]]"},
"classes": {"List[int]"},
},
}
def validate_json(jsonData):
try:
validate(instance=jsonData, schema=Emp_Schema)
except jsonschema.exceptions.ValidationError as error:
return False
return True
jsonStr = json.loads('''{
"boundaries": [
[
150.05322,
11.53223,
300.532,
123.1437
],
[
53.877,
332.663879,
49.9110756,
441.316406
],
[
388.617188,
298.171814,
401.498016,
308.061432
],
[
373.388794,
269.419434,
393.059235,
284.425232
],
[
388.449,
276.744293,
401.1651,
287.800415
],
[
371.061462,
69.9721527,
382.472076,
81.5727
],
[
354.491974,
147.660431,
370.116882,
160.380508
]
],
"connected": [
true,
false,
true,
true,
true,
true,
true
],
"compare": [
0.990648627,
0.989233553,
0.984282315,
0.949795425,
0.913895547,
0.539879441,
0.4897542
],
"center": [
[
369.494934,
116.291542
],
[
40.8455315,
332.990143
],
[
395.057587,
303.116638
],
[
383.224,
276.922333
],
[
394.807068,
282.272369
],
[
376.766785,
75.7724228
],
[
362.304443,
154.020477
]
],
"classes": [
0,
0,
0,
0,
0,
0,
0
]
}''')
# calling above define function
is_valid = validate_json(jsonStr)
if is_valid :
print('Given JSON data:\n ', jsonStr)
print("The JSON data is valid")
else:
print(jsonStr)
print("The JSON data is Invalid")
I am new to json format, so I bet i specified my Emp_Schema variable wrong. I would be grateful for any recommendations how to make it so, only the jsonStr would pass the test. Because the json string here is the string that should be valid, and any other should return as invalid.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
