I am writing a python program for validating json schema with sample JSON and I am using a package called jsonschema for JSON validation. When I run my code I am getting the error. It is showing that there is some is some issue in certain segment of JSON schema.But, I can’t make out what exactly the error means "jsonschema.exceptions.SchemaError:[Schema] is not of type ‘object’, ‘boolean’ " Below is the Python code:-
# import jsonSchema_validate
# from jsonSchema_validate import validate
from jsonschema import validate
schema = {"$schema":"http://json-schema.org/drafts/2020-12/schema","type":"object","properties":{"responseCode":{"type":"integer"},"message":{"type":"string"},"returnValueType":{"type":"string"},"returnValue":{"type":"object","properties":{"configuration":{"type":"object","properties":{"adminLocked":{"type":"object","properties":{"value":{"type":"string"},"statusCode":{"type":"integer"}}},"maxNumberOfMACAddresses":{"type":"object","properties":{"value":{"type":"integer"},"statusCode":{"type":"integer"}}},"port":{"type":"object","properties":{"value":{"type":"string"},"statusCode":{"type":"integer"}}},"qosProfile":{"type":"object","properties":{"value":{"type":"string"},"statusCode":{"type":"integer"}}},"operational":{"type":"object","properties":{"value":{"type":"string"},"statusCode":{"type":"integer"}}}}},"accessConnection":{"type":"object","properties":{"accessTechnology":{"type":"object","properties":{"value":{"type":"string"},"statusCode":{"type":"integer"}}},"linkUp":{"type":"object","properties":{"value":{"type":"boolean"},"statusCode":{"type":"integer"}}},"optical":{"type":"object","properties":{"upStream":{"type":"object","properties":{"waveLength":{"type":"object","properties":{"value":{"type":"integer"},"statusCode":{"type":"integer"}}},"signalPower":{"type":"object","properties":{"value":{"type":"string"},"statusCode":{"type":"integer"}}},"inRange":{"type":"object","properties":{"value":{"type":"boolean"},"statusCode":{"type":"integer"}}},"bytes":{"type":"object","properties":{"value":{"type":"string"},"statusCode":{"type":"integer"}}}}},"downStream":{"type":"object","properties":{"waveLength":{"type":"object","properties":{"value":{"type":"integer"},"statusCode":{"type":"integer"}}},"signalPower":{"type":"object","properties":{"value":{"type":"string"},"statusCode":{"type":"integer"}}},"inRange":{"type":"object","properties":{"value":{"type":"boolean"},"statusCode":{"type":"integer"}}},"bytes":{"type":"object","properties":{"value":{"type":"string"},"statusCode":{"type":"integer"}}}}}}},"lineError":{"type":"object","properties":{"upStream":{"type":"object","properties":{"value":{"type":"integer"},"statusCode":{"type":"integer"}}},"downStream":{"type":"object","properties":{"value":{"type":"integer"},"statusCode":{"type":"integer"}}}}}}},"terminationPoint":{"type":"object","properties":{"cpeType":{"type":"object","properties":{"value":{"type":"string"},"statusCode":{"type":"integer"}}},"serial":{"type":"object","properties":{"value":{"type":"string"},"statusCode":{"type":"integer"}}},"cpeUp":{"type":"object","properties":{"value":{"type":"boolean"},"statusCode":{"type":"integer"}}},"ethernet":{"type":"object","properties":{"linkUp":{"type":"object","properties":{"value":{"type":"boolean"},"statusCode":{"type":"integer"}}},"bitrate":{"type":"object","properties":{"value":{"type":"string"},"statusCode":{"type":"integer"}}},"fullDuplex":{"type":"object","properties":{"value":{"type":"boolean"},"statusCode":{"type":"integer"}}},"maxPortSpeed":{"type":"object","properties":{"value":{"type":"string"},"statusCode":{"type":"integer"}}}}},"upTime":{"type":"object","properties":{"value":{"type":"string"},"statusCode":{"type":"integer"}}}}},"rf":{"type":"object","properties":{"outputSignalStrength":{"type":"object","properties":{"value":{"type":"string"},"statusCode":{"type":"integer"}}},"filter":{"type":"object","properties":{"value":{"type":"string"},"statusCode":{"type":"integer"}}},"optical":{"type":"object","properties":{"downStream":{"type":"object","properties":{"waveLength":{"type":"object","properties":{"value":{"type":"integer"},"statusCode":{"type":"integer"}}},"signalPower":{"type":"object","properties":{"value":{"type":"integer"},"statusCode":{"type":"integer"}}},"inRange":{"type":"object","properties":{"value":{"type":"boolean"},"statusCode":{"type":"integer"}}}}}}}}},"vLans":{"type":"array","items":[{"type":"object","properties":{"cVlan":{"type":"object","properties":{"statusCode":{"type":"integer"},"value":{"type":"integer"}}},"learnedMACAddresses":{"type":"object","properties":{"statusCode":{"type":"integer"},"value":{"items":[{"properties":{"mac":{"type":"string"},"onPOI":{"properties":{"statusCode":{"type":"integer"},"value":{"type":"boolean"}},"type":"object"}},"type":"object"}],"type":"array"}}},"multicast":{"type":"object","properties":{"statusCode":{"type":"integer"},"value":{"type":"boolean"}}},"multicastSnoop":{"properties":{"items":[{"type":"string"}],"statusCode":{"type":"integer"},"type":"array"}},"tagged":{"type":"object","properties":{"statusCode":{"type":"integer"},"value":{"type":"boolean"}}},"tVlan":{"type":"object","properties":{"statusCode":{"type":"integer"},"value":{"type":"integer"}}},"whsid":{"type":"object","properties":{"statusCode":{"type":"integer"},"value":{"type":"string"}}}}}]},"poi":{"type":"object","properties":{"id":{"type":"object","properties":{"value":{"type":"string"},"statusCode":{"type":"integer"}}},"sVlan":{"type":"object","properties":{"value":{"type":"integer"},"statusCode":{"type":"integer"}}},"fPoi":{"type":"object","properties":{"value":{"type":"string"},"statusCode":{"type":"integer"}}}}}}}}}
validate(instance = {
"message": "7e",
"responseCode": -558638,
"returnValue": {
"accessConnection": {
"accessTechnology": {
"statusCode": -798758,
"value": "<"n!wrOJ,"
},
"lineError": {
"downStream": {
"value": -738597
}
}
},
"configuration": {
"adminLocked": {
"statusCode": 195229,
"value": "HM.7W0zhsZ"
},
"maxNumberOfMACAddresses": {
"statusCode": 495669,
"value": -800730
},
"operational": {},
"port": {
"statusCode": -672240
},
"qosProfile": {}
},
"poi": {
"fPoi": {
"value": "B}Hfm"
},
"id": {
"statusCode": 260531,
"value": "<G8|r"
}
},
"rf": {
"filter": {
"statusCode": 251614,
"value": "jYE"
},
"optical": {}
},
"terminationPoint": {
"cpeType": {
"statusCode": -896729,
"value": "G'?n"
},
"cpeUp": {
"statusCode": 606944
},
"ethernet": {
"bitrate": {
"statusCode": -318966
},
"linkUp": {},
"maxPortSpeed": {}
},
"serial": {
"statusCode": 365503,
"value": "rv?xkkb"
},
"upTime": {}
},
"vLans": [
]
},
"returnValueType": "&~857rS"
}, schema = schema)
# true
Below is the error received :
PS C:Usersabc> & C:/Users/abc/AppData/Local/Programs/Python/Python37/python.exe "c:/abc backup/D DRIVE DATA/Python Codes/jsonSchema_validate.py"
Traceback (most recent call last):
File "c:/abc backup/D DRIVE DATA/Python Codes/jsonSchema_validate.py", line 638, in <module>
}, schema=schema)
File "C:UsersabcAppDataLocalProgramsPythonPython37libsite-packagesjsonschemavalidators.py", line 1117, in validate
cls.check_schema(schema)
File "C:UsersabcAppDataLocalProgramsPythonPython37libsite-packagesjsonschemavalidators.py", line 231, in check_schema
raise exceptions.SchemaError.create_from(error)
jsonschema.exceptions.SchemaError: [{'type': 'object', 'properties': {'cVlan': {'type': 'object', 'properties': {'statusCode': {'type': 'integer'}, 'value': {'type': 'integer'}}}, 'learnedMACAddresses': {'type': 'object', 'properties': {'statusCode': {'type': 'integer'}, 'value': {'items': [{'properties': {'mac': {'type': 'string'}, 'onPOI': {'properties': {'statusCode': {'type': 'integer'}, 'value': {'type': 'boolean'}}, 'type': 'object'}}, 'type': 'object'}], 'type': 'array'}}}, 'multicast': {'type': 'object', 'properties': {'statusCode': {'type': 'integer'}, 'value': {'type': 'boolean'}}}, 'multicastSnoop': {'properties': {'items': [{'type': 'string'}], 'statusCode': {'type': 'integer'}, 'type': 'array'}}, 'tagged': {'type': 'object', 'properties': {'statusCode': {'type': 'integer'}, 'value': {'type': 'boolean'}}}, 'tVlan': {'type': 'object', 'properties': {'statusCode': {'type': 'integer'}, 'value': {'type': 'integer'}}}, 'whsid': {'type': 'object', 'properties': {'statusCode': {'type': 'integer'}, 'value': {'type': 'string'}}}}}] is not of type 'object', 'boolean'
PS C:Usersabc>
Can you please let me know how to solve this error.
2
Answers
The keyword
items
takes a schema, not an array of schemas. A schema is either an object or a boolean. You’re getting the error because the value is an array.You have also declared an invalid metaschema.
This is the correct one to use.