Reputation: 37
I am using fastjsonschema for validating json records against its schema. Some thing like this:
import fastjsonschema
validate = fastjsonschema.compile({'type': 'string'})
validate('hello')
If the json is valid, it returns the json string else return the error string. I just want to check if the json is valid or not. For this I can do a workaround of comparing output of the validate method and the json input.
But I want something cleaner. May be something like '$?' in unix or something better.
Could you suggest me?
Upvotes: -2
Views: 271
Reputation: 689
From the documentation, there seem to be two different exceptions thrown in case of error:
In Python, you can simply wrap that with a try ... except block like this:
try:
validate = fastjsonschema.compile({'type': 'string'})
validate(1)
except (fastjsonschema.JsonSchemaException, fastjsonschema.JsonSchemaDefinitionException):
print("Uh oh ...")
Upvotes: 0