How to validate a JSON in Python
How to validate a JSON in Python using JSON Schema. Edit on 13 Jan 2024: Pydantic looks to be a better alternative than the schema validation mentioned in the original post below. Please look into JSON - Pydantic to learn how to validate and parse a JSON using Pydantic. Here is a code snippet from the documentation: Original Post: JSONSchema validation is a powerful tool for validating JSON data. It helps ensure that the data you receive conforms to a particular schema or structure. With JSONSchema validation, you can define the expected format of the JSON data and then validate it against that schema. This is particularly useful when working with APIs or other data sources that provide JSON data. Here is an example of how to use JSONSchema validation in Python: First, we need to install the Next, we’ll create a JSON schema that defines the structure of our expected data. Here’s an example schema: This schema defines an object with four properties: name, age, email, and address. The name and age properties are required. The email property must be a valid email address. Now, let’s create some JSON data that we want to validate against this schema: Finally, we’ll use the That’s it! With just a few lines of code, we’ve used JSONSchema validation to ensure that our JSON data conform to a particular structure. This can be incredibly useful when working with APIs or other data sources that provide JSON data. This keyword is used to specify a list of schemas where at least one schema must validate the data. Here’s an example: This schema defines the data as a string or a number. If the data is a string, it must be a valid string; if it’s a number, it must be a valid number. This keyword is used to specify a list of schemas, where all of them must validate the data. Here’s an example: This schema defines the data as an object with the properties “name” and “age”. If any of these conditions are not met, the validation will fail. To validate against Here the field can be either a multiple of 3 or 5, like 100, 12, 18. However, the validation will fail if it happens to be a multiple of 3 and 5, like 15, 90, or 150. This keyword is used to specify a list of allowed values for the data. Here’s an example: This schema defines that the data must be a string and can only be one of the values “red,” “green,” or “blue.” Any other value will fail validation. These keywords are used to define conditional schemas. The This schema defines that if the This keyword is used to specify the type of data expected. Here’s an example: This schema defines that the data must be a string. This keyword is used to specify a format for a string or number. Here’s an example: This schema defines that the data must be a string in email format. This keyword is used to define an object’s properties and schemas. Here’s an example: This schema defines the data as an object with the properties “name” and “age”. The “name” property must be a string, and the “age” property must be an integer. This keyword is used to specify whether additional properties are allowed in an object. Here’s an example: This schema defines that only the “name” and “age” properties are allowed in the object. Any additional properties will not be permitted. To enable you can make the value This keyword is used to specify the schema of the items in an array. Here’s an example: This keyword is used to define the required properties of an object. Here’s an example: This schema defines the required “name” and “age” properties. The This keyword is used to specify a regular expression pattern that the data must match. Here’s an example: This schema defines that the data must be a string that matches the pattern of a social security number. The `minLength` keyword is used to specify the minimum length of a string, and `maxLength` for the maximum length. Here’s an example: This schema defines that the data must be a string of at least five characters and a maximum of 10 characters. To learn more about the possible keywords, follow the Understanding JSON Schema — Understanding JSON Schema 2020-12 documentation (json-schema.org).
=
:
:
=
#> when=datetime.date(1987, 1, 28) where=(51, -1)
"""
2 validation errors for Event
when
Input should be a valid date [type=date_type, input_value='1987-01-28', input_type=str]
where
Input should be a valid tuple [type=tuple_type, input_value=[51, -1], input_type=list]
"""
Introduction
jsonschema
Library. You can do this using pip:
schema =
data =
jsonschema
Library to validate our data against the schema:
=
=
=
return
JSON schema Keywords
anyOf
=
allOf
=
OneOf
oneOf
, the given data must be valid against exactly one of the given subschemas. Here’s an example:=
enum
=
if/else
if
schema is tested first, and if it validates the data, the then
schema is used. If the if
schema fails, the else
schema is used (if it exists). Here’s an example:=
is_employee
property is True
, the salary
property is required. If the is_employee
property is False
, the salary
property is not allowed. type
=
format
=
properties
=
additionalProperties
=
True`
items
=
required
=
minimum/maximum
minimum
keyword is used to specify the minimum value of a number. Similarly, the maximum
keyword is used for maximum value. Here’s an example:=
pattern
=
minLength/maxLength
=