JSON

JSON Schema

JSON Schema

JSON Schema is a vocabulary for annotating and validating JSON documents. It is a specification for JSON based format for defining the structure of JSON data. It describes your existing data format(s) and provides clear human- and machine- readable documentation. It validates data that is useful for automated testing and that in turn ensures quality of client submitted data. 

We call the JSON document that is under validation as the instance while the document containing the description is termed as the schema. The most basic schema is a blank JSON object, which constrains nothing, allows anything, and describes nothing. It is shown below:

{}

As per your requirement, you may apply constraints on an instance by adding validation keywords to the schema. For example, the “type” keyword can be used to restrict an instance to an object, array, string, number, boolean, or null:

{ "type": "string" }

 

Let us write a basic JSON schema with seven properties called keywords which are expressed as JSON keys to validate an employee record.

The $schema keyword states that this schema is written according to a specific draft of the standard and used for a variety of reasons, primarily version control.

The $id keyword defines a URI for the schema, and the base URI that other URI references within the schema are resolved against.

The title and description annotation keywords do not add constraints to the data being validated. They add readability.

The type validation keyword defines the first constraint on our JSON data and in this case it has to be a JSON Object. So our skeleton schema looks like this

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.jio.com/cloud_engineering/employee_record.schema.json",
  "title": "Employee record",
  "description": "An employee record",
  "type": "object"	
}

So, we have 2 schema Keywords i.e. $schema and $id; 2 schema Annotations i.e. title and description and 1 validation Keyword i.e., type. It’s time to add some properties and their validations.

No employee record is complete without its employee code and email id. These two properties are required. So let us add them.

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.jio.com/cloud_engineering/employee_record.schema.json",
  "title": "Employee record",
  "description": "An employee record",
  "type": "object"
  "properties": {
    "fname": {
      "description": "First name of the employee",
      "type": "string"
    }
    "lname": {
      "description": "Last name of the employee",
      "type": "string"
    }
    "employeeid": {
      "description": "The unique code given to each employee",
      "type": "integer"
    },
    "email-id": {
      "description": "Company official email-id of each employee",
      "type": "string"
    },
  "required": [ "fname", "lname", "employeeid", "email-id" ]  
}

A property is a validation term in JSON schema that must be an object. Each value of this object MUST be a valid JSON Schema. The required validator tells us that this property must be defined. The value of required keyword MUST be an array of unique strings. An object instance is valid against this keyword if every item in the array is the name of a property in the instance.

To explore further, one can read here