JSON Schema JSON schema notation example for validating arbitrary keys and dictionary types that value strings or numbers.
JSON-schema
{
"type": "object",
"additionalProperties": {
"anyOf": [
{"type": "string"},
{"type": "number"},
],
},
}
Example@python An example of running the above schema on Python using the jsonschema module.
import jsonschema
schema = {
"type": "object",
"additionalProperties": {
"anyOf": [
{"type": "string"},
{"type": "number"},
],
},
}
valid_obj = {
"key_a" : "value",
"key_b" : 1,
"key_c" : 1.1,
}
invalid_obj = {
"key_a" : "value",
"key_b" : 1,
"key_c" : None,
}
jsonschema.validate(valid_obj, schema)
try:
jsonschema.validate(invalid_obj, schema)
except jsonschema.ValidationError as e:
print(e)
None is not valid under any of the given schemas
Failed validating 'anyOf' in schema['additionalProperties']:
{'anyOf': [{'type': 'string'}, {'type': 'number'}]}
On instance['key_c']:
None
Dictionary-like JSON schema - Stack Overflow http://stackoverflow.com/questions/27357861/dictionary-like-json-schema
python - JSON schema validation with arbitrary keys - Stack Overflow http://stackoverflow.com/questions/16081118/json-schema-validation-with-arbitrary-keys
Recommended Posts