Python JSON Schema
Python-jsonschema
JSON: JSON stands for JavaScript Object Notation. It is a text-based format that represents structured data and can be used to interchange data among various applications. This is self-defining language and is easy to understand.
jsonschema: It is used to validate the JSON documents. The validated data that we get can be used for automated testing and ensuring that quality data is sent and received by the clients. It is human and machine-readable documentation. It is a contract that states that defines the data types and the format of the datatypes.
Jsonschema is the implementation of the specification of JSON schema in python.
Keyword | Definition and its function |
$schema | This specifies that the schema and its properties are defined as stated in the draft v4 specification. It ensures the compiler that the program is written follows all the stated The $schema keyword states that this schema is written according to the draft v4 specification. It can be used to declare the version of the JSON schema. |
title | It is used to add the title to your schema so that it can be identified for future use. |
description | It describes the various elements of the schema and its features. |
type | It states that Jsonschema can only be applied to JSON files and cannot be implemented on any other file. |
properties | It defines the various elements of the data structure, it contains all the key values and the minimum and maximum values that can be held by those keys. |
required | This keeps the track of all the properties that are must for the implementation of the schema. |
minimum | This is the constraint that is defined on the key, it specifies the minimum value held by the key any value below it is not expected. |
Exclusive minimum | It is exceptional and the user needs to decide to use it in schema or not, it takes Boolean values as input. If it is assigned True as the value, the value of the instance must be greater than the value passed to the minimum. |
maximum | This is the constraint that is defined on the key, it specifies the maximum value held by the key any value above it is not expected. |
exclusiveMaximum | It is also exceptional as the user decides to implement it in the schema or not, it takes Boolean values as input. If it is assigned True as the value, the value of the instance must be smaller than the value passed to the minimum. |
multipleOf | It determines whether the numeric value is the multiple of this keyword value or not, that is the result of this dividing the numeric instance by the keyword’s value should be an integral value. |
maxLength | It represents the maximum number of characters that can be stored in a string instance. It can be defined as the maximum length of the string. |
minLength | It represents the minimum number of characters that can be stored in a string instance. |
pattern | It matches the string instance and determines whether the string instance matches the sequence of characters of the regular expression. It is only valid if the string instance is a match for the regular instance. |
Json Schema Validation
Using the JSON schema makes the process of validating the API easier as you can construct the model of your API response. It enables the user to monitor the response of the API and ensure that the response we get is the same as the expected response.
It gives an alert when there is a breaking change in the response as the schema will not approve the data in the wrong format.
Implementing jsonschema in Python
To implement jsonschema you need to follow these basic steps each time:
- Install jsonschema in your system using pip
- Describe the schema of the JSON document
- Convert the received JSON file into an object of python using json.load() method
- This python object is then passed through the validate function, which will match the structure of the file from the structure defined in the schema. If the structure does not match, then it will throw an exception.
Installing the Jsonschema
It can be installed in the system like any other library of python using the pip installer. Type the “pip install jsonschema” command in the command prompt or the terminal of your system.
Validating the Data
Let us consider an example, we will validate a patient record from a hospital. Here the patient record is required to specify the name of the patient, patient id and the number of visits by the patient.
The JSON document must meet the following conditions to be a valid JSON:
- The patient name and patientid must be present in the JSON Data.
- Patient name should be in string format while the patientid and visits must be number.
import json
import jsonschema
from jsonschema import validate
# Describe the structure of the JSON document that you want to validate
patientSchema = {
"type": "object",
"properties": {
"name": {"type": "string"},
"patientid": {"type": "number"},
"doc_name": {"type": "string"},
"visits": {"type": "number"},
},
}
def validateJson(patientData):
try:
validate(instance=patientData, schema=patientSchema)
except jsonschema.exceptions.ValidationError as err:
return False
return True
# this method takes the json data and changes it to a python object.
patientData = json.loads('{"name": "Patient1", "patientid": "100","doc_name":"Doctor1", "visits": 3}')
# Now check whether the entered data is valid according to the schema
isValid = validateJson(patientData)
if isValid:
print(patientData)
print("The entered data is valid")
else:
print(patientData)
print("This data is invalid please enter the data again")
# Second JSON data converted to a Python object.
patientData = json.loads('{"name": "Patient2", "patientid": 101,"doc_name":"Doctor1", "visits": 5}')
# Performing validation on the second JSON data
isValid = validateJson(patientData)
if isValid:
print(patientData)
print("The entered data is valid")
else:
print(patientData)
print("This data is invalid please enter the data again")
CONCLUSION
The first JSON data is invalid as the patientid is a string, while in the schema we have defined its format as a number.
In the case of second data, all the corresponding data is in the same format as specified in the JSON schema.